config_file_fuzzer.c 1.16 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * libgit2 config file parser fuzz target.
 *
 * 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.
 */

10
#include "git2.h"
11
#include "config_backend.h"
12

13 14
#include "standalone_driver.h"

15 16
#define UNUSED(x) (void)(x)

17
static int foreach_cb(const git_config_entry *entry, void *payload)
Nelson Elhage committed
18
{
19 20 21
	UNUSED(entry);
	UNUSED(payload);

Nelson Elhage committed
22
	return 0;
23 24
}

25
int LLVMFuzzerInitialize(int *argc, char ***argv)
26
{
27 28 29 30 31 32 33 34 35 36 37 38
	UNUSED(argc);
	UNUSED(argv);

	if (git_libgit2_init() < 0)
		abort();

	return 0;
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
	git_config *cfg = NULL;
39
	git_config_backend *backend = NULL;
40 41
	int err = 0;

42
	if ((err = git_config_new(&cfg)) != 0) {
43
		goto out;
Nelson Elhage committed
44
	}
45

46
	if ((err = git_config_backend_from_string(&backend, (const char*)data, size)) != 0) {
47
		goto out;
Nelson Elhage committed
48
	}
49
	if ((err = git_config_add_backend(cfg, backend, 0, NULL, 0)) != 0) {
50
		goto out;
Nelson Elhage committed
51
	}
52
	/* Now owned by the config */
53 54 55 56 57 58
	backend = NULL;

	git_config_foreach(cfg, foreach_cb, NULL);
 out:
	git_config_backend_free(backend);
	git_config_free(cfg);
59

Nelson Elhage committed
60
	return 0;
61
}