Skip to content

Commit 6956a95

Browse files
committed
fuzzers: initialize libgit2 in standalone driver
The standalone driver for libgit2's fuzzing targets makes use of functions from libgit2 itself. While this is totally fine to do, we need to make sure to always have libgit2 initialized via `git_libgit2_init` before we call out to any of these. While this happens in most cases as we call `LLVMFuzzerInitialize`, which is provided by our fuzzers and which right now always calls `git_libgit2_init`, one exception to this rule is our error path when not enough arguments have been given. In this case, we will call `git_vector_free_deep` without libgit2 having been initialized. As we did not set up our allocation functions in that case, this will lead to a segmentation fault. Fix the issue by always initializing and shutting down libgit2 in the standalone driver. Note that we cannot let this replace the initialization in `LLVMFuzzerInitialize`, as it is required when using the "real" fuzzers by LLVM without our standalone driver. It's no problem to call the initialization and deinitialization functions multiple times, though.
1 parent a8d447f commit 6956a95

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

fuzzers/standalone_driver.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <stdlib.h>
1212
#include <sys/types.h>
1313

14+
#include "git2.h"
1415
#include "fileops.h"
1516
#include "path.h"
1617

@@ -41,6 +42,11 @@ int main(int argc, char **argv)
4142
unsigned i = 0;
4243
int error = 0;
4344

45+
if (git_libgit2_init() < 0) {
46+
fprintf(stderr, "Failed to initialize libgit2\n");
47+
abort();
48+
}
49+
4450
if (argc != 2) {
4551
fprintf(stderr, "Usage: %s <corpus directory>\n", argv[0]);
4652
error = -1;
@@ -66,5 +72,6 @@ int main(int argc, char **argv)
6672

6773
exit:
6874
git_vector_free_deep(&corpus_files);
75+
git_libgit2_shutdown();
6976
return error;
7077
}

0 commit comments

Comments
 (0)