Coverage Report

Created: 2026-10-23 07:09

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
5.65k
#define JANET_PEG_FUZZ_MAX_INPUT (1U << 20)
6
7
5.65k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
8
5.65k
    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
5.65k
    size_t patlen = (size_t)data[0] % (size - 1);
13
5.65k
    const uint8_t *patsrc = data + 1;
14
5.65k
    const uint8_t *text = data + 1 + patlen;
15
5.65k
    size_t textlen = size - 1 - patlen;
16
17
5.65k
    janet_init();
18
19
5.65k
    JanetParser parser;
20
5.65k
    janet_parser_init(&parser);
21
5.65k
    Janet matchfn = janet_resolve_core("peg/match");
22
5.65k
    if (janet_checktype(matchfn, JANET_CFUNCTION)) {
23
5.65k
        JanetCFunction cfun = janet_unwrap_cfunction(matchfn);
24
25
        /* Stop after parser errors: consuming again would panic. */
26
257k
        for (size_t i = 0; i < patlen; i++) {
27
252k
            janet_parser_consume(&parser, patsrc[i]);
28
252k
            if (janet_parser_status(&parser) == JANET_PARSE_ERROR)
29
737
                break;
30
252k
        }
31
5.65k
        if (janet_parser_status(&parser) != JANET_PARSE_ERROR)
32
4.91k
            janet_parser_eof(&parser);
33
34
5.65k
        if (janet_parser_status(&parser) != JANET_PARSE_ERROR &&
35
3.52k
                janet_parser_has_more(&parser)) {
36
3.43k
            Janet pattern = janet_parser_produce(&parser);
37
3.43k
            JanetTryState tstate;
38
3.43k
            if (janet_try(&tstate) == JANET_SIGNAL_OK) {
39
3.43k
                Janet argv[2];
40
3.43k
                argv[0] = pattern;
41
3.43k
                argv[1] = janet_stringv(text, (int32_t)textlen);
42
3.43k
                cfun(2, argv);
43
3.43k
            }
44
3.43k
            janet_restore(&tstate);
45
3.43k
        }
46
5.65k
    }
47
48
5.65k
    janet_parser_deinit(&parser);
49
5.65k
    janet_deinit();
50
5.65k
    return 0;
51
5.65k
}