/src/postgis/fuzzers/geohash_import_fuzzer.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: PostGIS |
4 | | * Purpose: GeoHash input fuzzer |
5 | | * |
6 | | ****************************************************************************** |
7 | | * Copyright (C) 2026 Darafei Praliaskouski <me@komzpa.net> |
8 | | * |
9 | | * This program is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU General Public License |
11 | | * as published by the Free Software Foundation; either version 2 |
12 | | * of the License, or (at your option) any later version. |
13 | | * |
14 | | ****************************************************************************/ |
15 | | |
16 | | #include <stddef.h> |
17 | | #include <stdint.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | extern "C" { |
22 | | #include "geos_stub.h" |
23 | | #include "proj_stub.h" |
24 | | #include "liblwgeom_internal.h" |
25 | | } |
26 | | |
27 | | #include "liblwgeom_fuzzer.hpp" |
28 | | |
29 | | extern "C" int |
30 | | LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) |
31 | 18 | { |
32 | 18 | postgis_lwgeom_fuzzer_initialize(); |
33 | 18 | return 0; |
34 | 18 | } |
35 | | |
36 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len); |
37 | | |
38 | | int |
39 | | LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) |
40 | 70 | { |
41 | 70 | if (len < 2) |
42 | 1 | return 0; |
43 | | |
44 | 69 | char *geohash = static_cast<char *>(malloc(len)); |
45 | 69 | if (geohash == NULL) |
46 | 0 | return 0; |
47 | | |
48 | 69 | memcpy(geohash, buf + 1, len - 1); |
49 | 69 | geohash[len - 1] = '\0'; |
50 | 69 | int precision = (buf[0] % 2) ? -1 : (int)(buf[0] % len); |
51 | 69 | double lat[2]; |
52 | 69 | double lon[2]; |
53 | | |
54 | 69 | if (!POSTGIS_LWGEOM_FUZZER_SETJMP()) |
55 | 69 | decode_geohash_bbox(geohash, lat, lon, precision); |
56 | 69 | postgis_lwgeom_fuzzer_cleanup_allocations(); |
57 | 69 | free(geohash); |
58 | 69 | return 0; |
59 | 69 | } |