Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logcheck: fix detection of invalid * regexp in filter #315

Merged
merged 1 commit into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions hack/tools/logcheck/pkg/filter.go
Expand Up @@ -88,8 +88,7 @@ func (f *RegexpFilter) Set(filename string) error {
line.enabled[c] = enabled
}

// Must match entire string.
re, err := regexp.Compile("^" + parts[1] + "$")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's on me that I didn't spot this :(

re, err := regexp.Compile(parts[1])
if err != nil {
return fmt.Errorf("%s:%d: %v", filename, lineNr, err)
}
Expand All @@ -106,11 +105,25 @@ func (f *RegexpFilter) Set(filename string) error {
// Enabled checks whether a certain check is enabled for a file.
func (f *RegexpFilter) Enabled(check string, enabled bool, filename string) bool {
for _, l := range f.lines {
if l.match.MatchString(filename) {
// Must match entire string.
if matchFullString(filename, l.match) {
if e, ok := l.enabled[check]; ok {
enabled = e
}
}
}
return enabled
}

func matchFullString(str string, re *regexp.Regexp) bool {
loc := re.FindStringIndex(str)
if loc == nil {
// No match at all.
return false
}
if loc[1]-loc[0] < len(str) {
// Only matches a substring.
return false
}
return true
}
6 changes: 5 additions & 1 deletion hack/tools/logcheck/pkg/filter_test.go
Expand Up @@ -116,7 +116,11 @@ func TestParsing(t *testing.T) {
}{
"invalid-regexp": {
content: `structured [`,
expectError: filename + ":0: error parsing regexp: missing closing ]: `[$`",
expectError: filename + ":0: error parsing regexp: missing closing ]: `[`",
},
"wildcard": {
content: `structured *`,
expectError: filename + ":0: error parsing regexp: missing argument to repetition operator: `*`",
},
"invalid-line": {
content: `structured .
Expand Down