Skip to content

Commit 5b11e3a

Browse files
scan: fix panic and fuzz flags (#823)
Flags used for fuzzing were constant due to a logic error. That in turn, prevented fuzzers from actually fuzzing. This commit fixes the fuzz flags and the panic.
1 parent 5621f29 commit 5b11e3a

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

‎internal/scan/bytes.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ func (b Bytes) Match(p []byte, flags Flags) int {
228228
return -1
229229
}
230230
b = b[1:]
231-
if !ByteIsWS(p[0]) {
231+
if len(p) > 0 && !ByteIsWS(p[0]) {
232232
b.TrimLWS()
233233
}
234234
} else {

‎internal/scan/bytes_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func FuzzSearch(f *testing.F) {
431431
}
432432
f.Fuzz(func(t *testing.T, haystack, needle []byte, flags int) {
433433
b := Bytes(haystack)
434-
b.Search(needle, Flags(flags)%CompactWS|IgnoreCase|FullWord)
434+
b.Search(needle, Flags(flags)&(CompactWS|IgnoreCase|FullWord))
435435
})
436436
}
437437

@@ -485,6 +485,9 @@ var matchTestcases = []struct {
485485
"fw+cws", "a bc d", "a bc", FullWord | CompactWS, 5,
486486
}, {
487487
"fw+ic+cws", "a bc d", "A BC", FullWord | IgnoreCase | CompactWS, 5,
488+
}, {
489+
// Pattern ending in whitespace used to panic with CompactWS.
490+
"cws trailing ws in pattern", "a ", "a ", CompactWS, 2,
488491
}}
489492

490493
func TestMatch(t *testing.T) {
@@ -504,7 +507,7 @@ func FuzzMatch(f *testing.F) {
504507
f.Add([]byte(tc.b), []byte(tc.p), int(tc.flags))
505508
}
506509
f.Fuzz(func(t *testing.T, b, p []byte, flags int) {
507-
Bytes(b).Match(p, Flags(flags)%CompactWS|IgnoreCase|FullWord)
510+
Bytes(b).Match(p, Flags(flags)&(CompactWS|IgnoreCase|FullWord))
508511
})
509512
}
510513

0 commit comments

Comments
 (0)