Coverage Report

Created: 2023-03-26 07:01

/src/fluent-bit/tests/internal/fuzzers/flb_json_fuzzer.c
Line
Count
Source (jump to first uncovered line)
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 <stdlib.h>
19
#include <stdint.h>
20
#include <fluent-bit/flb_pack.h>
21
#include <fluent-bit/flb_str.h>
22
#include "flb_fuzz_header.h"
23
24
int LLVMFuzzerTestOneInput(unsigned char *data, size_t size)
25
5.35k
{
26
5.35k
    TIMEOUT_GUARD
27
    /* Set fuzzer-malloc chance of failure */
28
5.34k
    flb_malloc_p = 0;
29
5.34k
    flb_malloc_mod = 25000;
30
31
5.34k
    if (size < 1) {
32
0
        return 0;
33
0
    }
34
5.34k
    unsigned char decider = *data;
35
5.34k
    data++;
36
5.34k
    size--;
37
38
    /* json packer */
39
5.34k
    char *out_buf = NULL;
40
5.34k
    size_t out_size;
41
5.34k
    int root_type;
42
5.34k
    int ret = flb_pack_json((char*)data, size, &out_buf, &out_size, &root_type);
43
44
5.34k
    if (ret == 0) {
45
4.75k
        size_t off = 0;
46
4.75k
        msgpack_unpacked result;
47
4.75k
        msgpack_unpacked_init(&result);
48
4.75k
        int ret2 = msgpack_unpack_next(&result, out_buf, out_size, &off);
49
4.75k
        if (ret2 == MSGPACK_UNPACK_SUCCESS) {
50
4.49k
            msgpack_object root = result.data;
51
4.49k
            char *tmp = NULL;
52
4.49k
            tmp = flb_msgpack_to_json_str(0, &root);
53
4.49k
            if (tmp != NULL) {
54
4.49k
                flb_free(tmp);
55
4.49k
            }
56
4.49k
        }
57
4.75k
        msgpack_unpacked_destroy(&result);
58
4.75k
        flb_sds_t d;
59
4.75k
        d = flb_sds_create("date");
60
4.75k
        if (decider < 0x30) {
61
2.20k
            flb_sds_t ret_s = flb_pack_msgpack_to_json_format(out_buf, out_size,
62
2.20k
                    FLB_PACK_JSON_FORMAT_LINES,
63
2.20k
                    (int)decider, d);
64
2.20k
            free(out_buf);
65
2.20k
            if (ret_s != NULL) {
66
932
                flb_sds_destroy(ret_s);
67
932
            }
68
2.20k
        }
69
2.54k
        else {
70
2.54k
            flb_sds_t ret_s = flb_pack_msgpack_to_json_format(out_buf, out_size,
71
2.54k
                    FLB_PACK_JSON_FORMAT_LINES,
72
2.54k
                    FLB_PACK_JSON_DATE_EPOCH, NULL);
73
2.54k
            free(out_buf);
74
2.54k
            if (ret_s != NULL) {
75
1.21k
                flb_sds_destroy(ret_s);
76
1.21k
            }
77
2.54k
        }
78
4.75k
        flb_sds_destroy(d);
79
4.75k
    }
80
81
5.34k
    return 0;
82
5.34k
}