/src/ndpi/fuzz/fuzz_alg_memmem.cpp
Line | Count | Source |
1 | | #include "ndpi_api.h" |
2 | | |
3 | | #include "fuzzer/FuzzedDataProvider.h" |
4 | | |
5 | 203 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
6 | 203 | FuzzedDataProvider fuzzed_data(data, size); |
7 | 203 | char dst[256]; |
8 | 203 | uint8_t *h; |
9 | 203 | int h_len, needle_len = 0, needle_start = 0; |
10 | | |
11 | | /* No real memory allocations involved */ |
12 | | |
13 | | /* 1: needle is a subset of haystack */ |
14 | | |
15 | 203 | std::vector<uint8_t>haystack = fuzzed_data.ConsumeBytes<uint8_t>(512); |
16 | 203 | h = haystack.data(); |
17 | 203 | h_len = haystack.size(); |
18 | | |
19 | 203 | if(h_len > 1) { |
20 | 202 | needle_start = fuzzed_data.ConsumeIntegralInRange(0, h_len - 1); |
21 | 202 | needle_len = fuzzed_data.ConsumeIntegralInRange(0, h_len - needle_start - 1); |
22 | 202 | } |
23 | 203 | ndpi_memmem(h, h_len, &h[needle_start], needle_len); |
24 | | |
25 | | /* 2: fully random */ |
26 | | |
27 | 203 | std::vector<uint8_t>needle = fuzzed_data.ConsumeBytes<uint8_t>(512); |
28 | 203 | ndpi_memmem(h, h_len, needle.data(), needle.size()); |
29 | | |
30 | | |
31 | | /* Let use this fuzzer to check also this simple function... */ |
32 | 203 | if(fuzzed_data.ConsumeBool()) |
33 | 72 | ndpi_strlcpy(dst, (const char *)h, sizeof(dst), h_len); |
34 | 131 | else |
35 | 131 | ndpi_strlcpy(NULL, (const char *)h, 0, h_len); |
36 | | |
37 | | /* Let use this fuzzer to check also this simple function... */ |
38 | 203 | ndpi_memcasecmp(fuzzed_data.ConsumeBool() ? h : NULL, |
39 | 203 | fuzzed_data.ConsumeBool() ? h : NULL, |
40 | 203 | fuzzed_data.ConsumeBool() ? h_len : 0); |
41 | | |
42 | 203 | return 0; |
43 | 203 | } |