Skip to content

Commit b246bed

Browse files
authored
Merge pull request libgit2#5269 from durin42/fuzzpatch
fuzzers: add a new fuzzer for patch parsing
2 parents c9464bf + 92e011a commit b246bed

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/fuzzers/patch_fuzzer.c b/fuzzers/patch_fuzzer.c
2+
index 76186b6fb..f7ce73ac8 100644
3+
--- a/fuzzers/patch_fuzzer.c
4+
+++ b/fuzzers/patch_fuzzer.c
5+
@@ -32,7 +32,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
6+
git_patch* patch;
7+
git_patch_options opts = {(uint32_t)data[0]};
8+
int status = git_patch_from_buffer(&patch, (const char*)data+1, size-1, &opts);
9+
- if (status == 0 && patch) {
10+
+ if (patch) {
11+
git_patch_free(patch);
12+
}
13+
return 0;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
diff --git a/fuzzers/patch_fuzzer.c b/fuzzers/patch_fuzzer.c
2+
new file mode 100644
3+
index 000000000..76186b6fb
4+
--- /dev/null
5+
+++ b/fuzzers/patch_fuzzer.c
6+
@@ -0,0 +1,39 @@
7+
+/*
8+
+ * libgit2 patch fuzzer target.
9+
+ *
10+
+ * Copyright (C) the libgit2 contributors. All rights reserved.
11+
+ *
12+
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
13+
+ * a Linking Exception. For full terms see the included COPYING file.
14+
+ */
15+
+
16+
+#include "git2.h"
17+
+#include "patch.h"
18+
+#include "patch_parse.h"
19+
+
20+
+#define UNUSED(x) (void)(x)
21+
+
22+
+int LLVMFuzzerInitialize(int *argc, char ***argv)
23+
+{
24+
+ UNUSED(argc);
25+
+ UNUSED(argv);
26+
+
27+
+ if (git_libgit2_init() < 0)
28+
+ abort();
29+
+
30+
+ return 0;
31+
+}
32+
+
33+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
34+
+{
35+
+ if (size < 1) {
36+
+ return 0;
37+
+ }
38+
+ git_patch* patch;
39+
+ git_patch_options opts = {(uint32_t)data[0]};
40+
+ int status = git_patch_from_buffer(&patch, (const char*)data+1, size-1, &opts);
41+
+ if (status == 0 && patch) {
42+
+ git_patch_free(patch);
43+
+ }
44+
+ return 0;
45+
+}

fuzzers/patch_parse_fuzzer.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* libgit2 patch parser fuzzer target.
3+
*
4+
* Copyright (C) the libgit2 contributors. All rights reserved.
5+
*
6+
* This file is part of libgit2, distributed under the GNU GPL v2 with
7+
* a Linking Exception. For full terms see the included COPYING file.
8+
*/
9+
10+
#include "git2.h"
11+
#include "patch.h"
12+
#include "patch_parse.h"
13+
14+
#define UNUSED(x) (void)(x)
15+
16+
int LLVMFuzzerInitialize(int *argc, char ***argv)
17+
{
18+
UNUSED(argc);
19+
UNUSED(argv);
20+
21+
if (git_libgit2_init() < 0)
22+
abort();
23+
24+
return 0;
25+
}
26+
27+
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
28+
{
29+
if (size) {
30+
git_patch *patch = NULL;
31+
git_patch_options opts = GIT_PATCH_OPTIONS_INIT;
32+
opts.prefix_len = (uint32_t)data[0];
33+
git_patch_from_buffer(&patch, (const char *)data + 1, size - 1,
34+
&opts);
35+
git_patch_free(patch);
36+
}
37+
return 0;
38+
}

0 commit comments

Comments
 (0)