Coverage Report

Created: 2025-04-11 06:29

/src/cjson/fuzzing/cjson_read_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
#include <stdlib.h>
2
#include <stdint.h>
3
#include <string.h>
4
5
#ifdef __cplusplus
6
extern "C" {
7
#endif
8
9
#include "../cJSON.h"
10
11
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); /* required by C89 */
12
13
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
14
2.03k
{
15
2.03k
    cJSON *json;
16
2.03k
    size_t offset = 4;
17
2.03k
    unsigned char *copied;
18
2.03k
    char *printed_json = NULL;
19
2.03k
    int minify, require_termination, formatted, buffered;
20
21
22
2.03k
    if(size <= offset) return 0;
23
2.02k
    if(data[size-1] != '\0') return 0;
24
2.01k
    if(data[0] != '1' && data[0] != '0') return 0;
25
2.00k
    if(data[1] != '1' && data[1] != '0') return 0;
26
1.98k
    if(data[2] != '1' && data[2] != '0') return 0;
27
1.97k
    if(data[3] != '1' && data[3] != '0') return 0;
28
29
1.96k
    minify              = data[0] == '1' ? 1 : 0;
30
1.96k
    require_termination = data[1] == '1' ? 1 : 0;
31
1.96k
    formatted           = data[2] == '1' ? 1 : 0;
32
1.96k
    buffered            = data[3] == '1' ? 1 : 0;
33
34
1.96k
    json = cJSON_ParseWithOpts((const char*)data + offset, NULL, require_termination);
35
36
1.96k
    if(json == NULL) return 0;
37
38
1.02k
    if(buffered)
39
532
    {
40
532
        printed_json = cJSON_PrintBuffered(json, 1, formatted);
41
532
    }
42
495
    else
43
495
    {
44
        /* unbuffered printing */
45
495
        if(formatted)
46
273
        {
47
273
            printed_json = cJSON_Print(json);
48
273
        }
49
222
        else
50
222
        {
51
222
            printed_json = cJSON_PrintUnformatted(json);
52
222
        }
53
495
    }
54
55
1.02k
    if(printed_json != NULL) free(printed_json);
56
57
1.02k
    if(minify)
58
838
    {
59
838
        copied = (unsigned char*)malloc(size);
60
838
        if(copied == NULL) return 0;
61
62
838
        memcpy(copied, data, size);
63
64
838
        cJSON_Minify((char*)copied + offset);
65
66
838
        free(copied);
67
838
    }
68
69
1.02k
    cJSON_Delete(json);
70
71
1.02k
    return 0;
72
1.02k
}
73
74
#ifdef __cplusplus
75
}
76
#endif
77