Debian Codegraph @

fuzz/fuzz_bstring.c

raw
1 /* Copyright (C) 2026 Daniel Markstedt <[email protected]>
2 * libFuzzer fuzz target for the Better String Library
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of bstrlib nor the names of its contributors may be
15 * used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Alternatively, the contents of this file may be used under the terms of
31 * GNU General Public License Version 2 (the "GPL").
32 */
33
34 /*
35 * Exercises the full public API of bstrlib and bstraux using raw bytes
36 * supplied by the fuzzer engine.
37 *
38 * Input layout
39 * ────────────
40 * data[0] fill character (gap-fill for insert / replace)
41 * data[1] split character (separator for bsplit)
42 * data[2] position hint (used modulo string length)
43 * data[3] length hint (used modulo a safe upper bound)
44 * data[4..mid] content of b0
45 * data[mid..] content of b1 (also used as find / replace target)
46 */
47
48 #include <stddef.h>
49 #include <stdint.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "bstraux.h"
54 #include "bstrlib.h"
55
56 #ifdef BSTRING_ENABLE_UTF8
57 #include "buniutil.h"
58 #include "utf8util.h"
59
60 /* Maximum UCS-2 output words allocated on the stack for buGetBlkUTF16. */
61 #define MAX_UCS2_OUT 256
62
63 static const cpUcs4 errch_table[] = {
64 0xFFFD, /* replacement character */
65 '?', /* ASCII fallback */
66 0, /* NUL */
67 ~(cpUcs4)0, /* abort sentinel */
68 };
69 #define ERRCH_TABLE_SZ ((int)(sizeof errch_table / sizeof errch_table[0]))
70 #endif /* BSTRING_ENABLE_UTF8 */
71
72 int
73 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
74 {
75 /* Need at least four control bytes to do anything interesting. */
76 if (size < 4) {
77 return 0;
78 }
79
80 unsigned char fill = data[0];
81 unsigned char split_ch = data[1];
82 int pos_hint = (int)data[2];
83 int len_hint = (int)data[3];
84
85 const uint8_t *payload = data + 4;
86 size_t psize = size - 4;
87 size_t mid = psize / 2;
88
89 /* Primary bstrings built directly from the fuzzer payload. */
90 bstring b0 = blk2bstr(payload, (int)mid);
91 bstring b1 = blk2bstr(payload + mid, (int)(psize - mid));
92
93 if (!b0 || !b1) {
94 bdestroy(b0);
95 bdestroy(b1);
96 return 0;
97 }
98
99 int slen0 = b0->slen;
100 int slen1 = b1->slen;
101
102 /* Clamp positional parameters to valid ranges. */
103 int safe_pos = slen0 > 0 ? pos_hint % (slen0 + 1) : 0;
104 int safe_len = (len_hint % 64) + 1; /* 1..64 */
105
106 /* ── Search and comparison (read-only) ─────────────────────────────── */
107
108 (void)binstr(b0, safe_pos, b1);
109 (void)binstrr(b0, slen0 > 0 ? slen0 - 1 : 0, b1);
110 (void)binstrcaseless(b0, safe_pos, b1);
111 (void)binstrrcaseless(b0, slen0 > 0 ? slen0 - 1 : 0, b1);
112 (void)bstrchrp(b0, split_ch, safe_pos);
113 (void)bstrrchrp(b0, split_ch, slen0 > 0 ? slen0 - 1 : 0);
114 (void)binchr(b0, 0, b1);
115 (void)binchrr(b0, slen0 > 0 ? slen0 - 1 : 0, b1);
116 (void)bninchr(b0, 0, b1);
117 (void)bninchrr(b0, slen0 > 0 ? slen0 - 1 : 0, b1);
118 (void)bstrcmp(b0, b1);
119 (void)bstricmp(b0, b1);
120 (void)bstrncmp(b0, b1, safe_len);
121 (void)bstrnicmp(b0, b1, safe_len);
122 (void)biseq(b0, b1);
123 (void)biseqcaseless(b0, b1);
124 (void)biseqblk(b0, payload, (int)mid);
125 (void)bisstemeqblk(b0, payload, (int)mid);
126 (void)bisstemeqcaselessblk(b0, payload, (int)mid);
127
128 /* ── Substring extraction ───────────────────────────────────────────── */
129
130 bstring sub = bmidstr(b0, safe_pos, safe_len);
131 bdestroy(sub);
132
133 bstring head = bHead(b0, safe_len);
134 bdestroy(head);
135
136 bstring tail = bTail(b0, safe_len);
137 bdestroy(tail);
138
139 /* ── NUL-terminated C-string round-trip ─────────────────────────────── */
140
141 char *cstr0 = bstr2cstr(b0, '?');
142
143 /* ── Encoding / decoding ────────────────────────────────────────────── */
144
145 bstring enc_sgml = bstrcpy(b0);
146 if (enc_sgml) {
147 (void)bSGMLEncode(enc_sgml);
148 bdestroy(enc_sgml);
149 }
150
151 bstring enc_b64 = bBase64Encode(b0);
152 bdestroy(enc_b64);
153
154 int b64_err = 0;
155 bstring dec_b64 = bBase64DecodeEx(b0, &b64_err);
156 bdestroy(dec_b64);
157
158 bstring enc_uu = bUuEncode(b0);
159 bdestroy(enc_uu);
160
161 int uu_bad = 0;
162 bstring dec_uu = bUuDecodeEx(b0, &uu_bad);
163 bdestroy(dec_uu);
164
165 bstring enc_yenc = bYEncode(b0);
166 bdestroy(enc_yenc);
167
168 bstring dec_yenc = bYDecode(b0);
169 bdestroy(dec_yenc);
170
171 /* ── Netstring parser (fuzzes parser with raw bytes as input) ───────── */
172
173 if (psize > 0) {
174 char *raw_cstr = malloc(psize + 1);
175 if (raw_cstr) {
176 memcpy(raw_cstr, payload, psize);
177 raw_cstr[psize] = '\0';
178 bstring fromnet = bNetStr2Bstr(raw_cstr);
179 bdestroy(fromnet);
180 free(raw_cstr);
181 }
182 }
183
184 char *netstr = bStr2NetStr(b0);
185 if (netstr) {
186 bstring roundtrip = bNetStr2Bstr(netstr);
187 bdestroy(roundtrip);
188 bcstrfree(netstr);
189 }
190
191 /* ── Split / join ───────────────────────────────────────────────────── */
192
193 struct bstrList *lst0 = bsplit(b0, split_ch);
194 if (lst0) {
195 bstring joined = bjoin(lst0, b1);
196 bdestroy(joined);
197 bstrListDestroy(lst0);
198 }
199
200 struct bstrList *lst1 = bsplits(b0, b1);
201 if (lst1) {
202 bstrListDestroy(lst1);
203 }
204
205 /* bsplitstr requires a non-empty separator. */
206 if (slen1 > 0) {
207 struct bstrList *lst2 = bsplitstr(b0, b1);
208 if (lst2) {
209 bstring joined2 = bjoin(lst2, b1);
210 bdestroy(joined2);
211 bstrListDestroy(lst2);
212 }
213 }
214
215 /* ── In-place mutation (each operates on a fresh copy of b0) ─────────── */
216
217 bstring work;
218
219 work = bstrcpy(b0);
220 if (work) {
221 (void)bconcat(work, b1);
222 bdestroy(work);
223 }
224
225 work = bstrcpy(b0);
226 if (work) {
227 (void)bcatblk(work, payload + mid, (int)(psize - mid));
228 bdestroy(work);
229 }
230
231 if (cstr0) {
232 work = bstrcpy(b0);
233 if (work) {
234 (void)bcatcstr(work, cstr0);
235 bdestroy(work);
236 }
237
238 work = bstrcpy(b1);
239 if (work) {
240 (void)bassigncstr(work, cstr0);
241 bdestroy(work);
242 }
243 }
244
245 work = bstrcpy(b0);
246 if (work) {
247 (void)bassignblk(work, payload + mid, (int)(psize - mid));
248 bdestroy(work);
249 }
250
251 work = bstrcpy(b0);
252 if (work) {
253 (void)binsert(work, safe_pos, b1, fill);
254 bdestroy(work);
255 }
256
257 work = bstrcpy(b0);
258 if (work) {
259 (void)binsertblk(work, safe_pos, payload, (int)mid, fill);
260 bdestroy(work);
261 }
262
263 work = bstrcpy(b0);
264 if (work) {
265 (void)binsertch(work, safe_pos, safe_len, fill);
266 bdestroy(work);
267 }
268
269 work = bstrcpy(b0);
270 if (work) {
271 (void)bdelete(work, safe_pos, safe_len);
272 bdestroy(work);
273 }
274
275 work = bstrcpy(b0);
276 if (work) {
277 (void)bsetstr(work, safe_pos, b1, fill);
278 bdestroy(work);
279 }
280
281 work = bstrcpy(b0);
282 if (work) {
283 (void)breplace(work, safe_pos, safe_len, b1, fill);
284 bdestroy(work);
285 }
286
287 work = bstrcpy(b0);
288 if (work) {
289 (void)btrunc(work, safe_len);
290 bdestroy(work);
291 }
292
293 work = bstrcpy(b0);
294 if (work) {
295 (void)btoupper(work);
296 bdestroy(work);
297 }
298
299 work = bstrcpy(b0);
300 if (work) {
301 (void)btolower(work);
302 bdestroy(work);
303 }
304
305 work = bstrcpy(b0);
306 if (work) {
307 (void)bltrimws(work);
308 bdestroy(work);
309 }
310
311 work = bstrcpy(b0);
312 if (work) {
313 (void)brtrimws(work);
314 bdestroy(work);
315 }
316
317 work = bstrcpy(b0);
318 if (work) {
319 (void)btrimws(work);
320 bdestroy(work);
321 }
322
323 work = bstrcpy(b0);
324 if (work) {
325 (void)bReverse(work);
326 bdestroy(work);
327 }
328
329 /* bpattern errors on empty strings; guard to avoid false crash reports. */
330 if (slen0 > 0) {
331 work = bstrcpy(b0);
332 if (work) {
333 (void)bpattern(work, safe_len + slen0);
334 bdestroy(work);
335 }
336 }
337
338 /* Cap repetitions to prevent OOM in CI. */
339 work = bstrcpy(b0);
340 if (work) {
341 (void)bReplicate(work, (len_hint % 8) + 1);
342 bdestroy(work);
343 }
344
345 /* bfindreplace: find operand must be non-empty. */
346 if (slen1 > 0) {
347 work = bstrcpy(b0);
348 if (work) {
349 (void)bfindreplace(work, b1, b0, 0);
350 bdestroy(work);
351 }
352
353 work = bstrcpy(b0);
354 if (work) {
355 (void)bfindreplacecaseless(work, b1, b0, 0);
356 bdestroy(work);
357 }
358 }
359
360 /* ── Justification (bstraux) ────────────────────────────────────────── */
361
362 int width = safe_len + slen0;
363
364 work = bstrcpy(b0);
365 if (work) {
366 (void)bJustifyLeft(work, ' ');
367 bdestroy(work);
368 }
369
370 work = bstrcpy(b0);
371 if (work) {
372 (void)bJustifyRight(work, width, ' ');
373 bdestroy(work);
374 }
375
376 work = bstrcpy(b0);
377 if (work) {
378 (void)bJustifyCenter(work, width, ' ');
379 bdestroy(work);
380 }
381
382 work = bstrcpy(b0);
383 if (work) {
384 (void)bJustifyMargin(work, width, ' ');
385 bdestroy(work);
386 }
387
388 bcstrfree(cstr0);
389 bdestroy(b0);
390 bdestroy(b1);
391
392 #ifdef BSTRING_ENABLE_UTF8
393 /*
394 * Unicode fuzzing
395 * ────────────────
396 * Reuses the same payload bytes already parsed above.
397 * data[0] → pos_hint (same byte as fill)
398 * data[1] → errch_sel (same byte as split_ch)
399 * payload[0..mid) → treated as raw UTF-8
400 * payload[mid..) → reinterpreted as UCS-2 / UCS-4 word arrays
401 */
402 {
403 unsigned char utf8_pos_hint = data[0];
404 unsigned char errch_sel = data[1];
405
406 const uint8_t *utf8_data = payload;
407 size_t utf8_size = mid;
408 const uint8_t *word_data = payload + mid;
409 size_t word_size = psize - mid;
410
411 cpUcs4 errCh = errch_table[errch_sel % ERRCH_TABLE_SZ];
412
413 /* ── utf8util: forward iterator (GetNext) ─────────────────────── */
414 {
415 struct utf8Iterator iter;
416 utf8IteratorInit(&iter, (unsigned char *)(uintptr_t)utf8_data,
417 (int)utf8_size);
418 while (!utf8IteratorNoMore(&iter)) {
419 (void)utf8IteratorGetNextCodePoint(&iter, errCh);
420 }
421 utf8IteratorUninit(&iter);
422 }
423
424 /* ── utf8util: peek iterator (GetCurr without advancing) ──────── */
425 {
426 struct utf8Iterator iter;
427 utf8IteratorInit(&iter, (unsigned char *)(uintptr_t)utf8_data,
428 (int)utf8_size);
429 if (!utf8IteratorNoMore(&iter)) {
430 (void)utf8IteratorGetCurrCodePoint(&iter, errCh);
431 }
432 utf8IteratorUninit(&iter);
433 }
434
435 /* ── utf8util: backward scanner ───────────────────────────────── */
436 if (utf8_size > 0) {
437 for (int i = 0; i < (int)utf8_size; i++) {
438 cpUcs4 out;
439 (void)utf8ScanBackwardsForCodePoint(utf8_data,
440 (int)utf8_size, i, &out);
441 }
442 int pos = (int)(utf8_pos_hint % utf8_size);
443 (void)utf8ScanBackwardsForCodePoint(utf8_data,
444 (int)utf8_size, pos, NULL);
445 }
446
447 /* ── buniutil: UTF-8 validation ───────────────────────────────── */
448 {
449 bstring bu = blk2bstr(utf8_data, (int)utf8_size);
450 if (bu) {
451 (void)buIsUTF8Content(bu);
452 bdestroy(bu);
453 }
454 }
455
456 /* ── buniutil: UTF-8 bstring → UTF-16 array ───────────────────── */
457 {
458 bstring bu = blk2bstr(utf8_data, (int)utf8_size);
459 if (bu) {
460 cpUcs2 out_buf[MAX_UCS2_OUT];
461 (void)buGetBlkUTF16(out_buf, MAX_UCS2_OUT, errCh, bu, 0);
462 if (utf8_size > 0) {
463 int pos = (int)(utf8_pos_hint % utf8_size);
464 (void)buGetBlkUTF16(out_buf, MAX_UCS2_OUT, errCh, bu, pos);
465 }
466 bdestroy(bu);
467 }
468 }
469
470 /* ── buniutil: UCS-4 array → UTF-8 bstring ───────────────────── */
471 if (word_size >= sizeof(cpUcs4)) {
472 int n = (int)(word_size / sizeof(cpUcs4));
473 cpUcs4 *ucs4_buf = malloc((size_t)n * sizeof(cpUcs4));
474 if (ucs4_buf) {
475 memcpy(ucs4_buf, word_data, (size_t)n * sizeof(cpUcs4));
476 bstring out = bfromcstr("");
477 if (out) {
478 (void)buAppendBlkUcs4(out, ucs4_buf, n, errCh);
479 bdestroy(out);
480 }
481 free(ucs4_buf);
482 }
483 }
484
485 /* ── buniutil: UTF-16 array → UTF-8 bstring ──────────────────── */
486 if (word_size >= sizeof(cpUcs2)) {
487 int n = (int)(word_size / sizeof(cpUcs2));
488 cpUcs2 *ucs2_buf = malloc((size_t)n * sizeof(cpUcs2));
489 if (ucs2_buf) {
490 memcpy(ucs2_buf, word_data, (size_t)n * sizeof(cpUcs2));
491
492 bstring out;
493
494 /* bom=NULL: no BOM tracking, native endianness assumed. */
495 out = bfromcstr("");
496 if (out) {
497 (void)buAppendBlkUTF16(out, ucs2_buf, n, NULL, errCh);
498 bdestroy(out);
499 }
500
501 /* *bom=0: let the function detect and consume a leading BOM. */
502 out = bfromcstr("");
503 if (out) {
504 cpUcs2 bom = 0;
505 (void)buAppendBlkUTF16(out, ucs2_buf, n, &bom, errCh);
506 bdestroy(out);
507 }
508
509 /* *bom=0xFEFF: caller pre-detected a little-endian BOM. */
510 out = bfromcstr("");
511 if (out) {
512 cpUcs2 bom = 0xFEFF;
513 (void)buAppendBlkUTF16(out, ucs2_buf, n, &bom, errCh);
514 bdestroy(out);
515 }
516
517 /* *bom=0xFFFE: caller pre-detected a big-endian BOM. */
518 out = bfromcstr("");
519 if (out) {
520 cpUcs2 bom = 0xFFFE;
521 (void)buAppendBlkUTF16(out, ucs2_buf, n, &bom, errCh);
522 bdestroy(out);
523 }
524
525 free(ucs2_buf);
526 }
527 }
528 }
529 #endif /* BSTRING_ENABLE_UTF8 */
530
531 return 0;
532 }