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") + } + + } +} +