Skip to content

Commit 93f711c

Browse files
committed
More fuzzing.
1 parent 341bd06 commit 93f711c

File tree

1 file changed

+105
-25
lines changed

1 file changed

+105
-25
lines changed

sqlite3/libc/libc_test.go

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,74 @@ func Test_strcasestr(t *testing.T) {
878878
}
879879
}
880880

881+
func Fuzz_memchr(f *testing.F) {
882+
f.Fuzz(func(t *testing.T, s string, c, i byte) {
883+
if len(s) > 128 || int(i) > len(s) {
884+
t.SkipNow()
885+
}
886+
copy(memory[ptr1:], s)
887+
888+
got := call(memchr, ptr1+uint64(i), uint64(c), uint64(len(s)-int(i)))
889+
want := strings.IndexByte(s[i:], c)
890+
if want >= 0 {
891+
want = ptr1 + int(i) + want
892+
} else {
893+
want = 0
894+
}
895+
896+
if uint32(got) != uint32(want) {
897+
t.Errorf("memchr(%q, %q) = %d, want %d",
898+
s[i:], c, uint32(got), uint32(want))
899+
}
900+
})
901+
}
902+
903+
func Fuzz_strchr(f *testing.F) {
904+
f.Fuzz(func(t *testing.T, s string, c, i byte) {
905+
if len(s) > 128 || int(i) > len(s) {
906+
t.SkipNow()
907+
}
908+
copy(memory[ptr1:], s)
909+
memory[ptr1+len(s)] = 0
910+
911+
got := call(strchr, ptr1+uint64(i), uint64(c))
912+
want := bytes.IndexByte(term1(memory[ptr1+uint64(i):]), c)
913+
if want >= 0 {
914+
want = ptr1 + int(i) + want
915+
} else {
916+
want = 0
917+
}
918+
919+
if uint32(got) != uint32(want) {
920+
t.Errorf("strchr(%q, %q) = %d, want %d",
921+
s[i:], c, uint32(got), uint32(want))
922+
}
923+
})
924+
}
925+
926+
func Fuzz_strrchr(f *testing.F) {
927+
f.Fuzz(func(t *testing.T, s string, c, i byte) {
928+
if len(s) > 128 || int(i) > len(s) {
929+
t.SkipNow()
930+
}
931+
copy(memory[ptr1:], s)
932+
memory[ptr1+len(s)] = 0
933+
934+
got := call(strrchr, ptr1+uint64(i), uint64(c))
935+
want := bytes.LastIndexByte(term1(memory[ptr1+uint64(i):]), c)
936+
if want >= 0 {
937+
want = ptr1 + int(i) + want
938+
} else {
939+
want = 0
940+
}
941+
942+
if uint32(got) != uint32(want) {
943+
t.Errorf("strrchr(%q, %q) = %d, want %d",
944+
s[i:], c, uint32(got), uint32(want))
945+
}
946+
})
947+
}
948+
881949
func Fuzz_memcmp(f *testing.F) {
882950
const s1 = compareTest1
883951
const s2 = compareTest2
@@ -935,10 +1003,10 @@ func Fuzz_strncmp(f *testing.F) {
9351003
const s2 = compareTest2
9361004

9371005
for i := range len(compareTest1) + 1 {
938-
f.Add(term(s1[i:]), term(s2[i:]), uint8(len(s1)))
1006+
f.Add(term(s1[i:]), term(s2[i:]), byte(len(s1)))
9391007
}
9401008

941-
f.Fuzz(func(t *testing.T, s1, s2 string, n uint8) {
1009+
f.Fuzz(func(t *testing.T, s1, s2 string, n byte) {
9421010
if len(s1) > 128 || len(s2) > 128 {
9431011
t.SkipNow()
9441012
}
@@ -993,10 +1061,10 @@ func Fuzz_strncasecmp(f *testing.F) {
9931061
const s2 = compareTest2
9941062

9951063
for i := range len(compareTest1) + 1 {
996-
f.Add(term(s1[i:]), term(s2[i:]), uint8(len(s1)))
1064+
f.Add(term(s1[i:]), term(s2[i:]), byte(len(s1)))
9971065
}
9981066

999-
f.Fuzz(func(t *testing.T, s1, s2 string, n uint8) {
1067+
f.Fuzz(func(t *testing.T, s1, s2 string, n byte) {
10001068
if len(s1) > 128 || len(s2) > 128 {
10011069
t.SkipNow()
10021070
}
@@ -1022,32 +1090,32 @@ func Fuzz_strspn(f *testing.F) {
10221090
f.Add(t.haystk, t.needle)
10231091
}
10241092

1025-
f.Fuzz(func(t *testing.T, text, chars string) {
1026-
if len(text) > 128 || len(chars) > 128 {
1093+
f.Fuzz(func(t *testing.T, s, chars string) {
1094+
if len(s) > 128 || len(chars) > 128 {
10271095
t.SkipNow()
10281096
}
1029-
copy(memory[ptr1:], text)
1097+
copy(memory[ptr1:], s)
10301098
copy(memory[ptr2:], chars)
1031-
memory[ptr1+len(text)] = 0
1099+
memory[ptr1+len(s)] = 0
10321100
memory[ptr2+len(chars)] = 0
10331101

10341102
got := call(strspn, uint64(ptr1), uint64(ptr2))
10351103

1036-
text = term(text)
1104+
s = term(s)
10371105
chars = term(chars)
1038-
want := strings.IndexFunc(text, func(r rune) bool {
1106+
want := strings.IndexFunc(s, func(r rune) bool {
10391107
if uint32(r) >= utf8.RuneSelf {
10401108
t.Skip()
10411109
}
10421110
return strings.IndexByte(chars, byte(r)) < 0
10431111
})
10441112
if want < 0 {
1045-
want = len(text)
1113+
want = len(s)
10461114
}
10471115

10481116
if uint32(got) != uint32(want) {
10491117
t.Errorf("strspn(%q, %q) = %d, want %d",
1050-
text, chars, uint32(got), uint32(want))
1118+
s, chars, uint32(got), uint32(want))
10511119
}
10521120
})
10531121
}
@@ -1057,32 +1125,32 @@ func Fuzz_strcspn(f *testing.F) {
10571125
f.Add(t.haystk, t.needle)
10581126
}
10591127

1060-
f.Fuzz(func(t *testing.T, text, chars string) {
1061-
if len(text) > 128 || len(chars) > 128 {
1128+
f.Fuzz(func(t *testing.T, s, chars string) {
1129+
if len(s) > 128 || len(chars) > 128 {
1130+
t.SkipNow()
1131+
}
1132+
if strings.ContainsFunc(chars, func(r rune) bool {
1133+
return uint32(r) >= utf8.RuneSelf
1134+
}) {
10621135
t.SkipNow()
10631136
}
1064-
copy(memory[ptr1:], text)
1137+
copy(memory[ptr1:], s)
10651138
copy(memory[ptr2:], chars)
1066-
memory[ptr1+len(text)] = 0
1139+
memory[ptr1+len(s)] = 0
10671140
memory[ptr2+len(chars)] = 0
10681141

10691142
got := call(strcspn, uint64(ptr1), uint64(ptr2))
10701143

1071-
text = term(text)
1144+
s = term(s)
10721145
chars = term(chars)
1073-
want := strings.IndexFunc(text, func(r rune) bool {
1074-
if uint32(r) >= utf8.RuneSelf {
1075-
t.Skip()
1076-
}
1077-
return strings.IndexByte(chars, byte(r)) >= 0
1078-
})
1146+
want := strings.IndexAny(s, chars)
10791147
if want < 0 {
1080-
want = len(text)
1148+
want = len(s)
10811149
}
10821150

10831151
if uint32(got) != uint32(want) {
10841152
t.Errorf("strcspn(%q, %q) = %d, want %d",
1085-
text, chars, uint32(got), uint32(want))
1153+
s, chars, uint32(got), uint32(want))
10861154
}
10871155
})
10881156
}
@@ -1184,6 +1252,9 @@ func Fuzz_strcasestr(f *testing.F) {
11841252
if len(haystk) > 128 || len(needle) > 128 {
11851253
t.SkipNow()
11861254
}
1255+
if len(needle) == 0 {
1256+
t.Skip("musl bug")
1257+
}
11871258
copy(memory[ptr1:], haystk)
11881259
copy(memory[ptr2:], needle)
11891260
memory[ptr1+len(haystk)] = 0
@@ -1241,3 +1312,12 @@ func term[T interface{ []byte | string }](s T) T {
12411312
}
12421313
return s
12431314
}
1315+
1316+
func term1[T interface{ []byte | string }](s T) T {
1317+
for i, c := range []byte(s) {
1318+
if c == 0 {
1319+
return s[:i+1]
1320+
}
1321+
}
1322+
return s
1323+
}

0 commit comments

Comments
 (0)