Skip to content

Commit 1a42b4c

Browse files
committed
Better fuzzing.
1 parent 7e4ec1d commit 1a42b4c

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

sqlite3/libc/libc_test.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"os"
99
"strings"
1010
"testing"
11-
"unicode/utf8"
1211

1312
"github.com/tetratelabs/wazero"
1413
"github.com/tetratelabs/wazero/api"
@@ -750,15 +749,7 @@ func Fuzz_strspn(f *testing.F) {
750749

751750
s = term(s)
752751
chars = term(chars)
753-
want := strings.IndexFunc(s, func(r rune) bool {
754-
if uint32(r) >= utf8.RuneSelf {
755-
t.Skip()
756-
}
757-
return strings.IndexByte(chars, byte(r)) < 0
758-
})
759-
if want < 0 {
760-
want = len(s)
761-
}
752+
want := indexNotByte(s, chars)
762753

763754
if uint32(got) != uint32(want) {
764755
t.Errorf("strspn(%v, %v) = %d, want %d",
@@ -778,11 +769,6 @@ func Fuzz_strcspn(f *testing.F) {
778769
if len(s) > 128 || len(chars) > 128 {
779770
t.SkipNow()
780771
}
781-
if strings.ContainsFunc(chars, func(r rune) bool {
782-
return uint32(r) >= utf8.RuneSelf
783-
}) {
784-
t.SkipNow()
785-
}
786772
copy(memory[ptr1:], s)
787773
copy(memory[ptr2:], chars)
788774
memory[ptr1+len(s)] = 0
@@ -792,10 +778,7 @@ func Fuzz_strcspn(f *testing.F) {
792778

793779
s = term(s)
794780
chars = term(chars)
795-
want := strings.IndexAny(s, chars)
796-
if want < 0 {
797-
want = len(s)
798-
}
781+
want := indexAnyByte(s, chars)
799782

800783
if uint32(got) != uint32(want) {
801784
t.Errorf("strcspn(%q, %q) = %d, want %d",
@@ -838,3 +821,21 @@ func term1[T interface{ []byte | string }](s T) T {
838821
}
839822
return s
840823
}
824+
825+
func indexNotByte(s, chars string) int {
826+
for i, c := range []byte(s) {
827+
if strings.IndexByte(chars, c) < 0 {
828+
return i
829+
}
830+
}
831+
return len(s)
832+
}
833+
834+
func indexAnyByte(s, chars string) int {
835+
for i, c := range []byte(s) {
836+
if strings.IndexByte(chars, c) >= 0 {
837+
return i
838+
}
839+
}
840+
return len(s)
841+
}

0 commit comments

Comments
 (0)