Просмотр исходного кода

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`.
Jakob Borg 6 лет назад
Родитель
Сommit
e0d4cdc9a3
2 измененных файлов с 22 добавлено и 0 удалено
  1. 4 0
      lib/ignore/ignore.go
  2. 18 0
      lib/ignore/ignore_test.go

+ 4 - 0
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)
 	}

+ 18 - 0
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)
+		}
+	}
+}