From b5ddc42b465dd5b9532ee336d98343d81a6d35b2 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 22 Oct 2020 12:11:29 -0400 Subject: io/fs, path, path/filepath, testing/fstest: validate patterns in Match, Glob According to #28614, proposal review agreed in December 2018 that Match should return an error for failed matches where the unmatched part of the pattern has a syntax error. (The failed match has to date caused the scan of the pattern to stop early.) This change implements that behavior: the match loop continues scanning to the end of the pattern, even after a confirmed mismatch, to check whether the pattern is even well-formed. The change applies to both path.Match and filepath.Match. Then filepath.Glob and fs.Glob make a single validity-checking call to Match before beginning their usual processing. Also update fstest.TestFS to check for correct validation in custom Glob implementations. Fixes #28614. Change-Id: Ic1d35a4bb9c3565184ae83dbefc425c5c96318e7 Reviewed-on: https://go-review.googlesource.com/c/go/+/264397 Trust: Russ Cox Reviewed-by: Rob Pike --- src/testing/fstest/testfs.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/testing') diff --git a/src/testing/fstest/testfs.go b/src/testing/fstest/testfs.go index 21cd00e5b6..4912a271b2 100644 --- a/src/testing/fstest/testfs.go +++ b/src/testing/fstest/testfs.go @@ -282,6 +282,12 @@ func (t *fsTester) checkGlob(dir string, list []fs.DirEntry) { glob = strings.Join(elem, "/") + "/" } + // Test that malformed patterns are detected. + // The error is likely path.ErrBadPattern but need not be. + if _, err := t.fsys.(fs.GlobFS).Glob(glob + "nonexist/[]"); err == nil { + t.errorf("%s: Glob(%#q): bad pattern not detected", dir, glob+"nonexist/[]") + } + // Try to find a letter that appears in only some of the final names. c := rune('a') for ; c <= 'z'; c++ { -- cgit v1.3-5-g9baa