/src/mercurial/contrib/fuzz/xdiff.cc
Line | Count | Source |
1 | | /* |
2 | | * xdiff.cc - fuzzer harness for thirdparty/xdiff |
3 | | * |
4 | | * Copyright 2018, Google Inc. |
5 | | * |
6 | | * This software may be used and distributed according to the terms of |
7 | | * the GNU General Public License, incorporated herein by reference. |
8 | | */ |
9 | | #include "thirdparty/xdiff/xdiff.h" |
10 | | #include <inttypes.h> |
11 | | #include <stdlib.h> |
12 | | |
13 | | #include "FuzzedDataProvider.h" |
14 | | |
15 | | extern "C" { |
16 | | |
17 | | int LLVMFuzzerInitialize(int *argc, char ***argv) |
18 | 16 | { |
19 | 16 | return 0; |
20 | 16 | } |
21 | | |
22 | | int hunk_consumer(long a1, long a2, long b1, long b2, void *priv) |
23 | 63.0k | { |
24 | | // TODO: probably also test returning -1 from this when things break? |
25 | 63.0k | return 0; |
26 | 63.0k | } |
27 | | |
28 | | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
29 | 2.95k | { |
30 | | // Don't allow fuzzer inputs larger than 100k, since we'll just bog |
31 | | // down and not accomplish much. |
32 | 2.95k | if (Size > 100000) { |
33 | 4 | return 0; |
34 | 4 | } |
35 | 2.94k | FuzzedDataProvider provider(Data, Size); |
36 | 2.94k | std::string left = provider.ConsumeRandomLengthString(Size); |
37 | 2.94k | std::string right = provider.ConsumeRemainingBytesAsString(); |
38 | 2.94k | mmfile_t a, b; |
39 | | |
40 | 2.94k | a.ptr = (char *)left.c_str(); |
41 | 2.94k | a.size = left.size(); |
42 | 2.94k | b.ptr = (char *)right.c_str(); |
43 | 2.94k | b.size = right.size(); |
44 | 2.94k | xpparam_t xpp = { |
45 | 2.94k | XDF_INDENT_HEURISTIC, /* flags */ |
46 | 2.94k | }; |
47 | 2.94k | xdemitconf_t xecfg = { |
48 | 2.94k | XDL_EMIT_BDIFFHUNK, /* flags */ |
49 | 2.94k | hunk_consumer, /* hunk_consume_func */ |
50 | 2.94k | }; |
51 | 2.94k | xdemitcb_t ecb = { |
52 | | NULL, /* priv */ |
53 | 2.94k | }; |
54 | 2.94k | xdl_diff(&a, &b, &xpp, &xecfg, &ecb); |
55 | 2.94k | return 0; // Non-zero return values are reserved for future use. |
56 | 2.95k | } |
57 | | |
58 | | } // extern "C" |