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`.
This commit is contained in:
Jakob Borg 2020-01-30 09:58:44 +01:00 committed by GitHub
parent ac3879e2b0
commit e0d4cdc9a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)
}
}
}