From 10d709b65896c13bedd47b4e074e5cab6359fb5f Mon Sep 17 00:00:00 2001 From: JB Baudens Date: Mon, 10 May 2021 13:04:06 -0600 Subject: [PATCH] introducing early check for pattern as done in filepath.Glob --- match.go | 4 ++++ match_test.go | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/match.go b/match.go index 7db4b7d..64d7e61 100644 --- a/match.go +++ b/match.go @@ -32,6 +32,10 @@ import ( // This was adapted from (http://golang.org/pkg/path/filepath) and uses several // built-ins from that package. func Glob(fs Fs, pattern string) (matches []string, err error) { + // Check pattern is well-formed. + if _, err := filepath.Match(pattern, ""); err != nil { + return nil, err + } if !hasMeta(pattern) { // Lstat not supported by a ll filesystems. if _, err = lstatIfPossible(fs, pattern); err != nil { diff --git a/match_test.go b/match_test.go index fa2c17b..c71ed83 100644 --- a/match_test.go +++ b/match_test.go @@ -174,9 +174,23 @@ func TestGlobSymlink(t *testing.T) { func TestGlobError(t *testing.T) { for _, fs := range Fss { - _, err := Glob(fs, "[7]") - if err != nil { + _, err := Glob(fs, "a/b[") + if err == nil { t.Error("expected error for bad pattern; got none") } } } + +func TestGlobErrorDirExistsAndContainsFile(t *testing.T) { + for _, fs := range Fss { + fs.Mkdir("a", os.ModePerm) + fs.OpenFile("a/a.txt", os.O_CREATE, os.ModePerm) + defer fs.RemoveAll("a") + _, err := Glob(fs, "a/b[") + if err == nil { + t.Error("expected error for bad pattern; got none") + } + + } +} +