Coverage Report

Created: 2025-07-04 07:08

/src/fluent-bit/tests/internal/fuzzers/parse_json_fuzzer.c
Line
Count
Source
1
/*  Fluent Bit
2
 *  ==========
3
 *  Copyright (C) 2019-2021 The Fluent Bit Authors
4
 *  Copyright (C) 2015-2018 Treasure Data Inc.
5
 *
6
 *  Licensed under the Apache License, Version 2.0 (the "License");
7
 *  you may not use this file except in compliance with the License.
8
 *  You may obtain a copy of the License at
9
 *
10
 *      http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 *  Unless required by applicable law or agreed to in writing, software
13
 *  distributed under the License is distributed on an "AS IS" BASIS,
14
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 *  See the License for the specific language governing permissions and
16
 *  limitations under the License.
17
 */
18
#include <stdint.h>
19
#include <string.h>
20
#include <stdlib.h>
21
#include <fluent-bit/flb_time.h>
22
#include <fluent-bit/flb_parser.h>
23
#include "flb_fuzz_header.h"
24
25
3.30k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size){
26
3.30k
    TIMEOUT_GUARD
27
3.29k
    void *out_buf = NULL;
28
3.29k
    size_t out_size = 0;
29
3.29k
    struct flb_time out_time;
30
3.29k
    struct flb_config *fuzz_config;
31
3.29k
    struct flb_parser *fuzz_parser;
32
33
    /* Set flb_malloc_mod to be fuzzer-data dependent */
34
3.29k
    if (size < 4) {
35
2
        return 0;
36
2
    }
37
3.29k
    flb_malloc_p = 0;
38
3.29k
    flb_malloc_mod = *(int*)data;
39
3.29k
    data += 4;
40
3.29k
    size -= 4;
41
42
    /* Avoid division by zero for modulo operations */
43
3.29k
    if (flb_malloc_mod == 0) {
44
1
        flb_malloc_mod = 1;
45
1
    }
46
47
    /* json parser */
48
3.29k
    fuzz_config = flb_config_init();
49
3.29k
    if (fuzz_config == NULL) {
50
152
        return 0;
51
152
    }
52
53
3.14k
    fuzz_parser = flb_parser_create("fuzzer", "json", NULL, FLB_TRUE, NULL,
54
3.14k
                                    NULL, NULL, MK_FALSE, MK_TRUE, FLB_FALSE, FLB_FALSE,
55
3.14k
                                    NULL, 0, NULL, fuzz_config);
56
3.14k
    if (fuzz_parser) {
57
3.13k
        flb_parser_do(fuzz_parser, (char*)data, size,
58
3.13k
                      &out_buf, &out_size, &out_time);
59
60
3.13k
        if (out_buf != NULL) {
61
18
            free(out_buf);
62
18
        }
63
64
3.13k
        flb_parser_destroy(fuzz_parser);
65
3.13k
    }
66
3.14k
    flb_config_exit(fuzz_config);
67
68
3.14k
    return 0;
69
3.29k
}