Coverage Report

Created: 2026-12-01 06:27

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
6.86k
#define JANET_PEG_FUZZ_MAX_INPUT (1U << 20)
6
7
6.86k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
8
6.86k
    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
6.86k
    size_t patlen = (size_t)data[0] % (size - 1);
13
6.86k
    const uint8_t *patsrc = data + 1;
14
6.86k
    const uint8_t *text = data + 1 + patlen;
15
6.86k
    size_t textlen = size - 1 - patlen;
16
17
6.86k
    janet_init();
18
19
6.86k
    JanetParser parser;
20
6.86k
    janet_parser_init(&parser);
21
6.86k
    Janet matchfn = janet_resolve_core("peg/match");
22
6.86k
    if (janet_checktype(matchfn, JANET_CFUNCTION)) {
23
6.86k
        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
250k
            janet_parser_consume(&parser, patsrc[i]);
28
250k
            if (janet_parser_status(&parser) == JANET_PARSE_ERROR)
29
505
                break;
30
250k
        }
31
6.86k
        if (janet_parser_status(&parser) != JANET_PARSE_ERROR)
32
6.36k
            janet_parser_eof(&parser);
33
34
6.86k
        if (janet_parser_status(&parser) != JANET_PARSE_ERROR &&
35
4.94k
                janet_parser_has_more(&parser)) {
36
4.85k
            Janet pattern = janet_parser_produce(&parser);
37
4.85k
            JanetTryState tstate;
38
4.85k
            if (janet_try(&tstate) == JANET_SIGNAL_OK) {
39
4.85k
                Janet argv[2];
40
4.85k
                argv[0] = pattern;
41
4.85k
                argv[1] = janet_stringv(text, (int32_t)textlen);
42
4.85k
                cfun(2, argv);
43
4.85k
            }
44
4.85k
            janet_restore(&tstate);
45
4.85k
        }
46
6.86k
    }
47
48
6.86k
    janet_parser_deinit(&parser);
49
6.86k
    janet_deinit();
50
6.86k
    return 0;
51
6.86k
}