xref: /freebsd/contrib/libedit/TEST/fuzz1.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * build:
3  *   CC=clang CXX=clang++ CFLAGS="-fsanitize=address,fuzzer-no-link -g" \
4  *   	CXXFLAGS="-fsanitize=address,fuzzer-no-link -g" ./configure && make
5  * run:
6  *   LD_LIBRARY_PATH=../src/.libs/ .libs/fuzz1 -max_len=32 \
7  *	-use_value_profile=1 -only_ascii=1
8  */
9 #include <readline/readline.h>
10 #include <locale.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 int init = 0;
17 
18 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
19   if (!Size)
20     return 0;
21 
22   if (!init) {
23     setlocale(LC_CTYPE, "");
24     stifle_history(7);
25     init = 1;
26   }
27 
28   clear_history();
29 
30   size_t lasti = 0;
31 
32   for (size_t i = 0;; ++i) {
33     if (i == Size || Data[i] == '\n') {
34       if (i - lasti) {
35         char *s = (char *)malloc(i - lasti + 1);
36         memcpy(s, &Data[lasti], i - lasti);
37         s[i - lasti] = '\0';
38 
39         char *expansion;
40         int result;
41 
42 #ifdef DEBUG
43         fprintf(stderr, "Calling history_expand: >%s<\n", s);
44 #endif
45         result = history_expand(s, &expansion);
46 
47         if (result < 0 || result == 2) {
48           /* Errors ignored */
49         } else {
50           add_history(expansion);
51         }
52         free(expansion);
53         free(s);
54       }
55       lasti = i + 1;
56     }
57 
58     if (i == Size)
59       break;
60   }
61 
62   return 0;
63 }
64