File: fuzz1.c

package info (click to toggle)
libedit 3.1-20250104-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,756 kB
  • sloc: ansic: 16,776; sh: 4,610; awk: 427; makefile: 134
file content (63 lines) | stat: -rw-r--r-- 1,290 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * build:
 *   CC=clang CXX=clang++ CFLAGS="-fsanitize=address,fuzzer-no-link -g" \
 *   	CXXFLAGS="-fsanitize=address,fuzzer-no-link -g" ./configure && make
 * run:
 *   LD_LIBRARY_PATH=../src/.libs/ .libs/fuzz1 -max_len=32 \
 *	-use_value_profile=1 -only_ascii=1
 */
#include <readline/readline.h>
#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int init = 0;

int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  if (!Size)
    return 0;

  if (!init) {
    setlocale(LC_CTYPE, "");
    stifle_history(7);
    init = 1;
  }

  clear_history();

  size_t lasti = 0;

  for (size_t i = 0;; ++i) {
    if (i == Size || Data[i] == '\n') {
      if (i - lasti) {
        char *s = (char *)malloc(i - lasti + 1);
        memcpy(s, &Data[lasti], i - lasti);
        s[i - lasti] = '\0';

        char *expansion;
        int result;

#ifdef DEBUG
        fprintf(stderr, "Calling history_expand: >%s<\n", s);
#endif
        result = history_expand(s, &expansion);

        if (result < 0 || result == 2) {
          /* Errors ignored */
        } else {
          add_history(expansion);
        }
        free(expansion);
        free(s);
      }
      lasti = i + 1;
    }

    if (i == Size)
      break;
  }

  return 0;
}