/src/geos/tests/fuzz/fuzz_geojson.c
Line | Count | Source |
1 | | #include <stdio.h> |
2 | | #include <stdlib.h> |
3 | | #include <stdint.h> |
4 | | #include <stdarg.h> |
5 | | #include <string.h> |
6 | | |
7 | | #include "geos_c.h" |
8 | | |
9 | | static FILE * flogOut; |
10 | | static GEOSGeoJSONReader *reader; |
11 | | |
12 | | void |
13 | 0 | notice(const char *fmt, ...) { |
14 | 0 | va_list ap; |
15 | 0 | fprintf( flogOut, "NOTICE: "); |
16 | 0 | va_start (ap, fmt); |
17 | 0 | vfprintf( flogOut, fmt, ap); |
18 | 0 | va_end(ap); |
19 | 0 | fprintf( flogOut, "\n" ); |
20 | 0 | } |
21 | | |
22 | | void |
23 | 5.74k | log_and_exit(const char *fmt, ...) { |
24 | 5.74k | va_list ap; |
25 | 5.74k | fprintf( flogOut, "ERROR: "); |
26 | 5.74k | va_start (ap, fmt); |
27 | 5.74k | vfprintf( flogOut, fmt, ap); |
28 | 5.74k | va_end(ap); |
29 | 5.74k | fprintf( flogOut, "\n" ); |
30 | 5.74k | } |
31 | | |
32 | 2 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
33 | 2 | (void)argc; (void)argv; |
34 | 2 | flogOut = fopen("/dev/null", "wb"); |
35 | 2 | initGEOS(notice, log_and_exit); |
36 | 2 | reader = GEOSGeoJSONReader_create(); |
37 | 2 | return 0; |
38 | 2 | } |
39 | | |
40 | 6.09k | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { |
41 | 6.09k | if (reader == NULL) { |
42 | 0 | return 0; |
43 | 0 | } |
44 | | |
45 | 6.09k | char *json = (char *) malloc(Size + 1); |
46 | 6.09k | if (json == NULL) { |
47 | 0 | return 0; |
48 | 0 | } |
49 | 6.09k | memcpy(json, Data, Size); |
50 | 6.09k | json[Size] = '\0'; |
51 | | |
52 | 6.09k | GEOSGeometry *g = GEOSGeoJSONReader_readGeometry(reader, json); |
53 | 6.09k | if (g != NULL) { |
54 | 357 | GEOSGeom_destroy(g); |
55 | 357 | } |
56 | | |
57 | 6.09k | free(json); |
58 | 6.09k | return 0; |
59 | 6.09k | } |