Coverage Report

Created: 2026-10-10 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/janet/test/fuzzers/fuzz_peg.c
Line
Count
Source
1
#include <stdint.h>
2
#include <stddef.h>
3
#include <janet.h>
4
5
2.66k
#define JANET_PEG_FUZZ_MAX_INPUT (1U << 20)
6
7
2.66k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
8
2.66k
    if (size < 2 || size > JANET_PEG_FUZZ_MAX_INPUT)
9
1
        return 0;
10
11
    /* Byte 0 selects the split point for a Janet PEG pattern and subject. */
12
2.66k
    size_t patlen = (size_t)data[0] % (size - 1);
13
2.66k
    const uint8_t *patsrc = data + 1;
14
2.66k
    const uint8_t *text = data + 1 + patlen;
15
2.66k
    size_t textlen = size - 1 - patlen;
16
17
2.66k
    janet_init();
18
19
2.66k
    JanetParser parser;
20
2.66k
    janet_parser_init(&parser);
21
2.66k
    Janet matchfn = janet_resolve_core("peg/match");
22
2.66k
    if (janet_checktype(matchfn, JANET_CFUNCTION)) {
23
2.66k
        JanetCFunction cfun = janet_unwrap_cfunction(matchfn);
24
25
        /* Stop after parser errors: consuming again would panic. */
26
86.9k
        for (size_t i = 0; i < patlen; i++) {
27
84.6k
            janet_parser_consume(&parser, patsrc[i]);
28
84.6k
            if (janet_parser_status(&parser) == JANET_PARSE_ERROR)
29
344
                break;
30
84.6k
        }
31
2.66k
        if (janet_parser_status(&parser) != JANET_PARSE_ERROR)
32
2.31k
            janet_parser_eof(&parser);
33
34
2.66k
        if (janet_parser_status(&parser) != JANET_PARSE_ERROR &&
35
1.52k
                janet_parser_has_more(&parser)) {
36
1.45k
            Janet pattern = janet_parser_produce(&parser);
37
1.45k
            JanetTryState tstate;
38
1.45k
            if (janet_try(&tstate) == JANET_SIGNAL_OK) {
39
1.45k
                Janet argv[2];
40
1.45k
                argv[0] = pattern;
41
1.45k
                argv[1] = janet_stringv(text, (int32_t)textlen);
42
1.45k
                cfun(2, argv);
43
1.45k
            }
44
1.45k
            janet_restore(&tstate);
45
1.45k
        }
46
2.66k
    }
47
48
2.66k
    janet_parser_deinit(&parser);
49
2.66k
    janet_deinit();
50
2.66k
    return 0;
51
2.66k
}