Code Monkey home page Code Monkey logo

Comments (6)

bmatcuk avatar bmatcuk commented on August 27, 2024 1

Hey @micimize! I'm so sorry I didn't see your reply back in May! I never got a notification from github that you replied! Your test cases were most helpful and I was able to fix the bug fairly quickly. I only wish I would have seen your reply a month ago so I could have fixed it then 😢

Anyway, I just released v4.0.2 with the fix. Let me know if you have any trouble with it.

By the way, doValidatePattern is only called if all else fails. Everything in the loop above it will either return or continue the loop before reaching that point. So, it will only reach that if the pattern has been valid up until now, but a match could not be made, and neither ** or * backtracking is possible. At that point, we already know we're returning false, we just want to know if we're also returning an error.

from doublestar.

micimize avatar micimize commented on August 27, 2024 1

will do! Sorry I haven't gotten around to actually testing it but will ping here if there are any issues when I do

from doublestar.

bmatcuk avatar bmatcuk commented on August 27, 2024

Hmm, are you using doublestar v4? What OS are you on? How are you building your pattern? You'll need to double the backslashes to get them to stay in the string, ie "**/log file \\{????-??-??\\}.txt".

I cannot reproduce this issue using v4 on macOS.

from doublestar.

micimize avatar micimize commented on August 27, 2024

I think I was maybe mistaken as to the exact issue – there is an issue with both \[ and \{ escaping.
If there is a match, they succeed, but if not, they return a syntax error. See https://play.golang.org/p/Saso864XooB

copied for reference
package main

import (
	"fmt"

	"github.com/bmatcuk/doublestar/v4"
)

func main() {
	patterns := []string{
		//`\{????-??-??\}.txt`,
		//`\[????-??-??\].txt`,
		`\{*\}.txt`,
		`\[*\].txt`,
	}
	names := []string{
		`{2021-05-01}.txt`,
		`[2021-05-01].txt`,
		`normal text`,
	}
	for _, p := range patterns {
		for _, n := range names {
			match, err := doublestar.Match(p, n)
			fmt.Println(p, n, match, err)
		}
	}

}

/* output
\{*\}.txt {2021-05-01}.txt true <nil>
\{*\}.txt [2021-05-01].txt false syntax error in pattern
\{*\}.txt normal text false syntax error in pattern
\[*\].txt {2021-05-01}.txt false syntax error in pattern
\[*\].txt [2021-05-01].txt true <nil>
\[*\].txt normal text false syntax error in pattern
*/

from doublestar.

micimize avatar micimize commented on August 27, 2024

@bmatcuk I found the issue – every iteration of the loop consuming pattern validates the remaining pattern:

doublestar/match.go

Lines 279 to 281 in 1fd2e2d

if validate && patIdx < patLen && !doValidatePattern(pattern[patIdx:], separator) {
return false, ErrBadPattern
}

The default fallthrough after '\\' is matched breaks before bumping patIdx when a match is rejected, resulting in an invalid pattern at validation time:

doublestar/match.go

Lines 241 to 245 in 1fd2e2d

if patRune != nameRune {
break
}
patIdx += patRuneLen

Why validate the pattern every loop? doValidatePattern is called nameLen * patLen times this way

from doublestar.

bmatcuk avatar bmatcuk commented on August 27, 2024

I'm going to close this issue since there hasn't been any activity. Let me know if you run into trouble with the fix!

from doublestar.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.