MCPcopy Create free account
hub / github.com/DaveGamble/cJSON / LLVMFuzzerTestOneInput

Function LLVMFuzzerTestOneInput

fuzzing/cjson_read_fuzzer.c:13–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size); /* required by C89 */
12
13int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
14{
15 cJSON *json;
16 size_t offset = 4;
17 unsigned char *copied;
18 char *printed_json = NULL;
19 int minify, require_termination, formatted, buffered;
20
21
22 if(size <= offset) return 0;
23 if(data[size-1] != '\0') return 0;
24 if(data[0] != '1' && data[0] != '0') return 0;
25 if(data[1] != '1' && data[1] != '0') return 0;
26 if(data[2] != '1' && data[2] != '0') return 0;
27 if(data[3] != '1' && data[3] != '0') return 0;
28
29 minify = data[0] == '1' ? 1 : 0;
30 require_termination = data[1] == '1' ? 1 : 0;
31 formatted = data[2] == '1' ? 1 : 0;
32 buffered = data[3] == '1' ? 1 : 0;
33
34 json = cJSON_ParseWithOpts((const char*)data + offset, NULL, require_termination);
35
36 if(json == NULL) return 0;
37
38 if(buffered)
39 {
40 printed_json = cJSON_PrintBuffered(json, 1, formatted);
41 }
42 else
43 {
44 /* unbuffered printing */
45 if(formatted)
46 {
47 printed_json = cJSON_Print(json);
48 }
49 else
50 {
51 printed_json = cJSON_PrintUnformatted(json);
52 }
53 }
54
55 if(printed_json != NULL) free(printed_json);
56
57 if(minify)
58 {
59 copied = (unsigned char*)malloc(size);
60 if(copied == NULL) return 0;
61
62 memcpy(copied, data, size);
63
64 cJSON_Minify((char*)copied + offset);
65
66 free(copied);
67 }
68
69 cJSON_Delete(json);
70

Callers 1

mainFunction · 0.85

Calls 6

cJSON_ParseWithOptsFunction · 0.85
cJSON_PrintBufferedFunction · 0.85
cJSON_PrintFunction · 0.85
cJSON_PrintUnformattedFunction · 0.85
cJSON_MinifyFunction · 0.85
cJSON_DeleteFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…