New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path/filepath: always validate full Glob patterns #28614

Open
fstab opened this Issue Nov 6, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@fstab

fstab commented Nov 6, 2018

The functions filepath.Match(pattern, name) and path.Match(pattern, name) return ErrBadPattern if the pattern is invalid, but due to lazy evaluation the error may or may not occur depending on what you match.

Example:

_, err := filepath.Match("a[", "a") // err is nil
_, err := filepath.Match("a[", "abc") // err is "syntax error in pattern"

As patterns are often user input, it would be handy to be able to validate the pattern. If the validation is successful, it should be guaranteed that filepath.Match() will not return an error.

I created a PR for this here and was asked to open a proposal issue first.

Other programming languages seem to either not validate the pattern at all (like Python's glob.glob('a[')), or always validate the pattern (like Java's FileSystems.getDefault().getPathMatcher("glob: a["))). I couldn't find an example where validation depends on what name is matched.

@tklauser tklauser changed the title from [Proposal] Function to validate Glob patterns to Proposal: path/filepath: function to validate Glob patterns Nov 6, 2018

@gopherbot gopherbot added this to the Proposal milestone Nov 6, 2018

@gopherbot gopherbot added the Proposal label Nov 6, 2018

@ianlancetaylor ianlancetaylor changed the title from Proposal: path/filepath: function to validate Glob patterns to proposal: path/filepath: function to validate Glob patterns Nov 6, 2018

@opennota

This comment has been minimized.

opennota commented Nov 7, 2018

An alternative: make Match evaluate pattern non-lazily. That way there would be no risk that the implementations will diverge and Match will return an error for pattern which passes IsPatternValid.

@andybons andybons changed the title from proposal: path/filepath: function to validate Glob patterns to path/filepath: always validate full Glob patterns Dec 5, 2018

@andybons

This comment has been minimized.

Member

andybons commented Dec 5, 2018

Per discussion with @golang/proposal-review, we believe that the Match function should evaluate non-lazily (@opennota's suggestion) so that one can use Match to validate a Glob without adding a new function.

@andybons andybons added NeedsFix and removed Proposal labels Dec 5, 2018

@andybons andybons modified the milestones: Proposal, Go1.13 Dec 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment