Add proper error checking on regexps
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func loadV1Config(content []byte) (*ZurgConfigV1, error) {
|
||||
func loadV1Config(content []byte, log *zap.SugaredLogger) (*ZurgConfigV1, error) {
|
||||
var configV1 ZurgConfigV1
|
||||
if err := yaml.Unmarshal(content, &configV1); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
configV1.log = log
|
||||
return &configV1, nil
|
||||
}
|
||||
|
||||
@@ -84,7 +86,11 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentIDs, fileNames []s
|
||||
}
|
||||
}
|
||||
if filter.RegexStr != "" {
|
||||
regex := compilePattern(filter.RegexStr)
|
||||
regex, err := compilePattern(filter.RegexStr)
|
||||
if err != nil {
|
||||
z.log.Errorf("Failed to compile regex: %v", err)
|
||||
return false
|
||||
}
|
||||
if regex.MatchString(torrentName) {
|
||||
return true
|
||||
}
|
||||
@@ -119,7 +125,11 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentIDs, fileNames []s
|
||||
}
|
||||
}
|
||||
if filter.FileInsideRegexStr != "" {
|
||||
regex := compilePattern(filter.FileInsideRegexStr)
|
||||
regex, err := compilePattern(filter.FileInsideRegexStr)
|
||||
if err != nil {
|
||||
z.log.Errorf("Failed to compile regex: %v", err)
|
||||
return false
|
||||
}
|
||||
for _, filename := range fileNames {
|
||||
if regex.MatchString(filename) {
|
||||
return true
|
||||
@@ -143,7 +153,7 @@ func (z *ZurgConfigV1) matchFilter(torrentName string, torrentIDs, fileNames []s
|
||||
return false
|
||||
}
|
||||
|
||||
func compilePattern(pattern string) *regexp.Regexp {
|
||||
func compilePattern(pattern string) (*regexp.Regexp, error) {
|
||||
flags := map[rune]string{
|
||||
'i': "(?i)",
|
||||
'm': "(?m)",
|
||||
@@ -173,8 +183,8 @@ func compilePattern(pattern string) *regexp.Regexp {
|
||||
|
||||
// Validate pattern
|
||||
if finalPattern == "" || finalPattern == flagString {
|
||||
return nil
|
||||
return nil, fmt.Errorf("invalid regex pattern: %s", pattern)
|
||||
}
|
||||
|
||||
return regexp.MustCompile(finalPattern)
|
||||
return regexp.Compile(finalPattern)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user