Mercurial > hg
annotate contrib/fuzz/xdiff.cc @ 38173:fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Alex Gaynor suggested we should probably copy the left and right sides
of diffs to new blocks so we can detect over-reads in the diffing
code, and I agree. Once I got into that, I realized we should do
things with C++17 idioms rather than keep using malloc() and
free(). This change is the result. I tried to split it more than this
and failed.
Everything still compiles and works in the oss-fuzz container, so I
think we can count on C++17 being available!
Differential Revision: https://phab.mercurial-scm.org/D3675
author | Augie Fackler <augie@google.com> |
---|---|
date | Sat, 28 Apr 2018 22:18:50 -0400 |
parents | 04d64163039a |
children | 2e60a77b7058 |
rev | line source |
---|---|
36679 | 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 | |
38173
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
13 #include "fuzzutil.h" |
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
14 |
36679 | 15 extern "C" { |
16 | |
17 int hunk_consumer(long a1, long a2, long b1, long b2, void *priv) | |
18 { | |
19 // TODO: probably also test returning -1 from this when things break? | |
20 return 0; | |
21 } | |
22 | |
23 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) | |
24 { | |
38173
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
25 auto maybe_inputs = SplitInputs(Data, Size); |
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
26 if (!maybe_inputs) { |
36679 | 27 return 0; |
28 } | |
38173
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
29 auto inputs = std::move(maybe_inputs.value()); |
36679 | 30 mmfile_t a, b; |
31 | |
38173
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
32 a.ptr = inputs.left.get(); |
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
33 a.size = inputs.left_size; |
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
34 b.ptr = inputs.right.get(); |
fa0ddd5e8fff
fuzz: extract some common utilities and use modern C++ idioms
Augie Fackler <augie@google.com>
parents:
36765
diff
changeset
|
35 b.size = inputs.right_size; |
36679 | 36 xpparam_t xpp = { |
37 XDF_INDENT_HEURISTIC, /* flags */ | |
38 }; | |
39 xdemitconf_t xecfg = { | |
40 XDL_EMIT_BDIFFHUNK, /* flags */ | |
41 hunk_consumer, /* hunk_consume_func */ | |
42 }; | |
43 xdemitcb_t ecb = { | |
44 NULL, /* priv */ | |
45 }; | |
46 xdl_diff(&a, &b, &xpp, &xecfg, &ecb); | |
47 return 0; // Non-zero return values are reserved for future use. | |
48 } | |
49 | |
50 #ifdef HG_FUZZER_INCLUDE_MAIN | |
51 int main(int argc, char **argv) | |
52 { | |
53 const char data[] = "asdf"; | |
54 return LLVMFuzzerTestOneInput((const uint8_t *)data, 4); | |
55 } | |
56 #endif | |
57 | |
58 } // extern "C" |