mirror of
https://github.com/libgit2/libgit2.git
synced 2026-01-25 02:56:17 +00:00
fuzzers: provide util functions like repo init
Many fuzzers will need to operate with a repository; extract the repository initialization from downloads_refs_fuzzer.c into its own utility area.
This commit is contained in:
@@ -12,10 +12,13 @@ foreach(fuzz_target_src ${SRC_FUZZERS})
|
||||
string(REPLACE ".c" "" fuzz_target_name ${fuzz_target_src})
|
||||
string(REPLACE "_fuzzer" "" fuzz_name ${fuzz_target_name})
|
||||
|
||||
set(${fuzz_target_name}_SOURCES ${fuzz_target_src} ${LIBGIT2_OBJECTS})
|
||||
set(${fuzz_target_name}_SOURCES
|
||||
${fuzz_target_src} "fuzzer_utils.c" ${LIBGIT2_OBJECTS})
|
||||
|
||||
if(USE_STANDALONE_FUZZERS)
|
||||
list(APPEND ${fuzz_target_name}_SOURCES "standalone_driver.c")
|
||||
endif()
|
||||
|
||||
add_executable(${fuzz_target_name} ${${fuzz_target_name}_SOURCES})
|
||||
set_target_properties(${fuzz_target_name} PROPERTIES C_STANDARD 90)
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "futils.h"
|
||||
|
||||
#include "standalone_driver.h"
|
||||
#include "fuzzer_utils.h"
|
||||
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
@@ -157,33 +158,10 @@ static int fuzzer_transport_cb(git_transport **out, git_remote *owner, void *par
|
||||
return git_transport_smart(out, owner, &def);
|
||||
}
|
||||
|
||||
static void fuzzer_git_abort(const char *op)
|
||||
{
|
||||
const git_error *err = git_error_last();
|
||||
fprintf(stderr, "unexpected libgit error: %s: %s\n",
|
||||
op, err ? err->message : "<none>");
|
||||
abort();
|
||||
}
|
||||
|
||||
int LLVMFuzzerInitialize(int *argc, char ***argv)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
char tmpdir[MAX_PATH], path[MAX_PATH];
|
||||
|
||||
if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
|
||||
abort();
|
||||
|
||||
if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
|
||||
abort();
|
||||
|
||||
if (git_futils_mkdir(path, 0700, 0) < 0)
|
||||
abort();
|
||||
#else
|
||||
char path[] = "/tmp/git2.XXXXXX";
|
||||
|
||||
if (mkdtemp(path) != path)
|
||||
abort();
|
||||
#endif
|
||||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
|
||||
if (git_libgit2_init() < 0)
|
||||
abort();
|
||||
@@ -191,12 +169,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
|
||||
if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
|
||||
abort();
|
||||
|
||||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
|
||||
if (git_repository_init(&repo, path, 1) < 0)
|
||||
fuzzer_git_abort("git_repository_init");
|
||||
|
||||
repo = fuzzer_repo_init();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
51
fuzzers/fuzzer_utils.c
Normal file
51
fuzzers/fuzzer_utils.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (C) the libgit2 contributors. All rights reserved.
|
||||
*
|
||||
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
||||
* a Linking Exception. For full terms see the included COPYING file.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "git2.h"
|
||||
#include "futils.h"
|
||||
|
||||
#include "fuzzer_utils.h"
|
||||
|
||||
void fuzzer_git_abort(const char *op)
|
||||
{
|
||||
const git_error *err = git_error_last();
|
||||
fprintf(stderr, "unexpected libgit error: %s: %s\n",
|
||||
op, err ? err->message : "<none>");
|
||||
abort();
|
||||
}
|
||||
|
||||
git_repository *fuzzer_repo_init(void)
|
||||
{
|
||||
git_repository *repo;
|
||||
|
||||
#if defined(_WIN32)
|
||||
char tmpdir[MAX_PATH], path[MAX_PATH];
|
||||
|
||||
if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
|
||||
abort();
|
||||
|
||||
if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
|
||||
abort();
|
||||
|
||||
if (git_futils_mkdir(path, 0700, 0) < 0)
|
||||
abort();
|
||||
#else
|
||||
char path[] = "/tmp/git2.XXXXXX";
|
||||
|
||||
if (mkdtemp(path) != path)
|
||||
abort();
|
||||
#endif
|
||||
|
||||
if (git_repository_init(&repo, path, 1) < 0)
|
||||
fuzzer_git_abort("git_repository_init");
|
||||
|
||||
return repo;
|
||||
}
|
||||
14
fuzzers/fuzzer_utils.h
Normal file
14
fuzzers/fuzzer_utils.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (C) the libgit2 contributors. All rights reserved.
|
||||
*
|
||||
* This file is part of libgit2, distributed under the GNU GPL v2 with
|
||||
* a Linking Exception. For full terms see the included COPYING file.
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_fuzzer_utils_h__
|
||||
#define INCLUDE_fuzzer_utils_h__
|
||||
|
||||
extern void fuzzer_git_abort(const char *op);
|
||||
extern git_repository *fuzzer_repo_init(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user