Mercurial > hg
comparison contrib/fuzz/xdiff.cc @ 36679:624cbd1477a6
fuzz: add a fuzzer for xdiff
Based entirely on the fuzzer for bdiff.
Differential Revision: https://phab.mercurial-scm.org/D2632
author | Augie Fackler <augie@google.com> |
---|---|
date | Sat, 03 Mar 2018 18:58:13 -0500 |
parents | |
children | 04d64163039a |
comparison
equal
deleted
inserted
replaced
36678:7834927f0243 | 36679:624cbd1477a6 |
---|---|
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 extern "C" { | |
14 | |
15 int hunk_consumer(long a1, long a2, long b1, long b2, void *priv) | |
16 { | |
17 // TODO: probably also test returning -1 from this when things break? | |
18 return 0; | |
19 } | |
20 | |
21 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) | |
22 { | |
23 if (!Size) { | |
24 return 0; | |
25 } | |
26 // figure out a random point in [0, Size] to split our input. | |
27 size_t split = Data[0] / 255.0 * Size; | |
28 | |
29 mmfile_t a, b; | |
30 | |
31 // `a` input to diff is data[1:split] | |
32 a.ptr = (char *)Data + 1; | |
33 // which has len split-1 | |
34 a.size = split - 1; | |
35 // `b` starts at the next byte after `a` ends | |
36 b.ptr = a.ptr + a.size; | |
37 b.size = Size - split; | |
38 xpparam_t xpp = { | |
39 XDF_INDENT_HEURISTIC, /* flags */ | |
40 NULL, /* anchors */ | |
41 0, /* anchors_nr */ | |
42 }; | |
43 xdemitconf_t xecfg = { | |
44 0, /* ctxlen */ | |
45 0, /* interhunkctxlen */ | |
46 XDL_EMIT_BDIFFHUNK, /* flags */ | |
47 NULL, /* find_func */ | |
48 NULL, /* find_func_priv */ | |
49 hunk_consumer, /* hunk_consume_func */ | |
50 }; | |
51 xdemitcb_t ecb = { | |
52 NULL, /* priv */ | |
53 NULL, /* outf */ | |
54 }; | |
55 xdl_diff(&a, &b, &xpp, &xecfg, &ecb); | |
56 return 0; // Non-zero return values are reserved for future use. | |
57 } | |
58 | |
59 #ifdef HG_FUZZER_INCLUDE_MAIN | |
60 int main(int argc, char **argv) | |
61 { | |
62 const char data[] = "asdf"; | |
63 return LLVMFuzzerTestOneInput((const uint8_t *)data, 4); | |
64 } | |
65 #endif | |
66 | |
67 } // extern "C" |