From e0d4cdc9a338ffa1d46385d3997bb214748aa259 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Thu, 30 Jan 2020 09:58:44 +0100 Subject: [PATCH] lib/ignore: Don't crash on empty patterns (fixes #6300) (#6301) Instead of panicking when we try to look closer at the empty pattern, return something like `invalid pattern "(?d)" in ignore file: missing pattern`. --- lib/ignore/ignore.go | 4 ++++ lib/ignore/ignore_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/ignore/ignore.go b/lib/ignore/ignore.go index 7578446ba..cbc56b13d 100644 --- a/lib/ignore/ignore.go +++ b/lib/ignore/ignore.go @@ -422,6 +422,10 @@ func parseLine(line string) ([]Pattern, error) { } } + if line == "" { + return nil, errors.New("missing pattern") + } + if pattern.result.IsCaseFolded() { line = strings.ToLower(line) } diff --git a/lib/ignore/ignore_test.go b/lib/ignore/ignore_test.go index 462f88a31..1de1e305d 100644 --- a/lib/ignore/ignore_test.go +++ b/lib/ignore/ignore_test.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "testing" "time" @@ -1150,3 +1151,20 @@ func TestSkipIgnoredDirs(t *testing.T) { t.Error("SkipIgnoredDirs should be true") } } + +func TestEmptyPatterns(t *testing.T) { + // These patterns are all invalid and should be rejected as such (without panicking...) + tcs := []string{ + "!", + "(?d)", + "(?i)", + } + + for _, tc := range tcs { + m := New(fs.NewFilesystem(fs.FilesystemTypeFake, "")) + err := m.Parse(strings.NewReader(tc), ".stignore") + if err == nil { + t.Error("Should reject invalid pattern", tc) + } + } +}