/src/postgis/fuzzers/twkb_import_fuzzer.cpp
Line | Count | Source |
1 | | /****************************************************************************** |
2 | | * |
3 | | * Project: PostGIS |
4 | | * Purpose: TWKB 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 | | |
19 | | extern "C" { |
20 | | #include "geos_stub.h" |
21 | | #include "proj_stub.h" |
22 | | } |
23 | | |
24 | | #include "liblwgeom_fuzzer.hpp" |
25 | | |
26 | | extern "C" int |
27 | | LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) |
28 | 18 | { |
29 | 18 | postgis_lwgeom_fuzzer_initialize(); |
30 | 18 | return 0; |
31 | 18 | } |
32 | | |
33 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len); |
34 | | |
35 | | int |
36 | | LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) |
37 | 3.07k | { |
38 | | /* Avoid inputs too short to contain TWKB metadata. If the metadata says an |
39 | | * extended precision byte follows, make sure that byte exists too. |
40 | | */ |
41 | 3.07k | if (len < 2) |
42 | 1 | return 0; |
43 | 3.07k | if ((buf[1] & 0x08) && len < 3) |
44 | 1 | return 0; |
45 | | |
46 | 3.07k | if (POSTGIS_LWGEOM_FUZZER_SETJMP()) |
47 | 2.04k | { |
48 | 2.04k | postgis_lwgeom_fuzzer_cleanup_allocations(); |
49 | 2.04k | return 0; |
50 | 2.04k | } |
51 | | |
52 | 1.02k | LWGEOM *lwgeom = lwgeom_from_twkb(buf, len, LW_PARSER_CHECK_NONE); |
53 | 1.02k | lwgeom_free(lwgeom); |
54 | 1.02k | postgis_lwgeom_fuzzer_cleanup_allocations(); |
55 | 1.02k | return 0; |
56 | 3.07k | } |