/src/gpsd/fuzzer/FuzzJson.c
Line | Count | Source |
1 | | /* Copyright 2022 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 "gpsd_config.h" |
14 | | |
15 | | #include <stdio.h> |
16 | | #include <stdlib.h> |
17 | | |
18 | | #include "gpsd.h" |
19 | | #include "gps_json.h" |
20 | | |
21 | 5.34k | #define kMinInputLength 10 |
22 | 2.66k | #define kMaxInputLength 5120 |
23 | | |
24 | | static struct gps_data_t gpsdata; |
25 | | |
26 | | extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
27 | 2.67k | {//gpsd/tests//test_json.c |
28 | | |
29 | 2.67k | if (Size < kMinInputLength || Size > kMaxInputLength){ |
30 | 25 | return 0; |
31 | 25 | } |
32 | | |
33 | 2.64k | uint8_t *DataFx; |
34 | 2.64k | size_t SizeFx = Size+1; |
35 | 2.64k | DataFx = (uint8_t *)calloc(SizeFx,sizeof(uint8_t)); |
36 | | |
37 | 2.64k | memcpy((void *)DataFx,(void *)Data,Size); |
38 | | |
39 | 2.64k | char AddCB[] ={0x7b}; //{ |
40 | 2.64k | memcpy((void *)DataFx,(void *)AddCB,sizeof(AddCB)); |
41 | | //calloc already added 0x00 at the end of DataFx. |
42 | | |
43 | 2.64k | int status; |
44 | 2.64k | { |
45 | 2.64k | memset((void *)&gpsdata, 0, sizeof(gpsdata)); |
46 | 2.64k | status = libgps_json_unpack((char *)DataFx, &gpsdata, NULL); |
47 | 2.64k | } |
48 | 2.64k | { |
49 | 2.64k | memset((void *)&gpsdata, 0, sizeof(gpsdata)); |
50 | 2.64k | status = json_toff_read((char *)DataFx, &gpsdata, NULL); |
51 | 2.64k | } |
52 | | |
53 | 2.64k | free(DataFx); |
54 | | |
55 | 2.64k | return status; |
56 | 2.67k | } |