Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2023 Google LLC |
2 | | Licensed under the Apache License, Version 2.0 (the "License"); |
3 | | you may not use this file except in compliance with the License. |
4 | | You may obtain a copy of the License at |
5 | | http://www.apache.org/licenses/LICENSE-2.0 |
6 | | Unless required by applicable law or agreed to in writing, software |
7 | | distributed under the License is distributed on an "AS IS" BASIS, |
8 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
9 | | See the License for the specific language governing permissions and |
10 | | limitations under the License. |
11 | | */ |
12 | | |
13 | | #include <stdint.h> |
14 | | #include <stdio.h> |
15 | | #include <stdlib.h> |
16 | | |
17 | | #include "cJSON.h" |
18 | | #include "loader.h" |
19 | | #include "fuzz_header.h" |
20 | | |
21 | | /* |
22 | | * Targets the custom version of cJson. |
23 | | */ |
24 | 2.80k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
25 | 2.80k | char filename[256]; |
26 | 2.80k | sprintf(filename, "/tmp/libfuzzer.%d", getpid()); |
27 | | |
28 | 2.80k | FILE *fp = fopen(filename, "wb"); |
29 | 2.80k | if (!fp) { |
30 | 0 | return 0; |
31 | 0 | } |
32 | 2.80k | fwrite(data, size, 1, fp); |
33 | 2.80k | fclose(fp); |
34 | | |
35 | 2.80k | cJSON *json = NULL; |
36 | 2.80k | loader_get_json(NULL, filename, &json); |
37 | | |
38 | 2.80k | if (json == NULL) { |
39 | 1.02k | goto out; |
40 | 1.02k | } |
41 | 1.77k | bool out_of_mem = false; |
42 | 1.77k | char *json_data = loader_cJSON_Print(json, &out_of_mem); |
43 | | |
44 | 1.77k | if (json_data != NULL) { |
45 | 1.77k | free(json_data); |
46 | 1.77k | } |
47 | | |
48 | 1.77k | if (json != NULL) { |
49 | 1.77k | loader_cJSON_Delete(json); |
50 | 1.77k | } |
51 | | |
52 | 2.80k | out: |
53 | 2.80k | unlink(filename); |
54 | | |
55 | 2.80k | return 0; |
56 | 1.77k | } |