@@ -878,6 +878,74 @@ func Test_strcasestr(t *testing.T) {
878
878
}
879
879
}
880
880
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
+
881
949
func Fuzz_memcmp (f * testing.F ) {
882
950
const s1 = compareTest1
883
951
const s2 = compareTest2
@@ -935,10 +1003,10 @@ func Fuzz_strncmp(f *testing.F) {
935
1003
const s2 = compareTest2
936
1004
937
1005
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 )))
939
1007
}
940
1008
941
- f .Fuzz (func (t * testing.T , s1 , s2 string , n uint8 ) {
1009
+ f .Fuzz (func (t * testing.T , s1 , s2 string , n byte ) {
942
1010
if len (s1 ) > 128 || len (s2 ) > 128 {
943
1011
t .SkipNow ()
944
1012
}
@@ -993,10 +1061,10 @@ func Fuzz_strncasecmp(f *testing.F) {
993
1061
const s2 = compareTest2
994
1062
995
1063
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 )))
997
1065
}
998
1066
999
- f .Fuzz (func (t * testing.T , s1 , s2 string , n uint8 ) {
1067
+ f .Fuzz (func (t * testing.T , s1 , s2 string , n byte ) {
1000
1068
if len (s1 ) > 128 || len (s2 ) > 128 {
1001
1069
t .SkipNow ()
1002
1070
}
@@ -1022,32 +1090,32 @@ func Fuzz_strspn(f *testing.F) {
1022
1090
f .Add (t .haystk , t .needle )
1023
1091
}
1024
1092
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 {
1027
1095
t .SkipNow ()
1028
1096
}
1029
- copy (memory [ptr1 :], text )
1097
+ copy (memory [ptr1 :], s )
1030
1098
copy (memory [ptr2 :], chars )
1031
- memory [ptr1 + len (text )] = 0
1099
+ memory [ptr1 + len (s )] = 0
1032
1100
memory [ptr2 + len (chars )] = 0
1033
1101
1034
1102
got := call (strspn , uint64 (ptr1 ), uint64 (ptr2 ))
1035
1103
1036
- text = term (text )
1104
+ s = term (s )
1037
1105
chars = term (chars )
1038
- want := strings .IndexFunc (text , func (r rune ) bool {
1106
+ want := strings .IndexFunc (s , func (r rune ) bool {
1039
1107
if uint32 (r ) >= utf8 .RuneSelf {
1040
1108
t .Skip ()
1041
1109
}
1042
1110
return strings .IndexByte (chars , byte (r )) < 0
1043
1111
})
1044
1112
if want < 0 {
1045
- want = len (text )
1113
+ want = len (s )
1046
1114
}
1047
1115
1048
1116
if uint32 (got ) != uint32 (want ) {
1049
1117
t .Errorf ("strspn(%q, %q) = %d, want %d" ,
1050
- text , chars , uint32 (got ), uint32 (want ))
1118
+ s , chars , uint32 (got ), uint32 (want ))
1051
1119
}
1052
1120
})
1053
1121
}
@@ -1057,32 +1125,32 @@ func Fuzz_strcspn(f *testing.F) {
1057
1125
f .Add (t .haystk , t .needle )
1058
1126
}
1059
1127
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
+ }) {
1062
1135
t .SkipNow ()
1063
1136
}
1064
- copy (memory [ptr1 :], text )
1137
+ copy (memory [ptr1 :], s )
1065
1138
copy (memory [ptr2 :], chars )
1066
- memory [ptr1 + len (text )] = 0
1139
+ memory [ptr1 + len (s )] = 0
1067
1140
memory [ptr2 + len (chars )] = 0
1068
1141
1069
1142
got := call (strcspn , uint64 (ptr1 ), uint64 (ptr2 ))
1070
1143
1071
- text = term (text )
1144
+ s = term (s )
1072
1145
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 )
1079
1147
if want < 0 {
1080
- want = len (text )
1148
+ want = len (s )
1081
1149
}
1082
1150
1083
1151
if uint32 (got ) != uint32 (want ) {
1084
1152
t .Errorf ("strcspn(%q, %q) = %d, want %d" ,
1085
- text , chars , uint32 (got ), uint32 (want ))
1153
+ s , chars , uint32 (got ), uint32 (want ))
1086
1154
}
1087
1155
})
1088
1156
}
@@ -1184,6 +1252,9 @@ func Fuzz_strcasestr(f *testing.F) {
1184
1252
if len (haystk ) > 128 || len (needle ) > 128 {
1185
1253
t .SkipNow ()
1186
1254
}
1255
+ if len (needle ) == 0 {
1256
+ t .Skip ("musl bug" )
1257
+ }
1187
1258
copy (memory [ptr1 :], haystk )
1188
1259
copy (memory [ptr2 :], needle )
1189
1260
memory [ptr1 + len (haystk )] = 0
@@ -1241,3 +1312,12 @@ func term[T interface{ []byte | string }](s T) T {
1241
1312
}
1242
1313
return s
1243
1314
}
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