config_file_fuzzer.c 1.12 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
#define UNUSED(x) (void)(x)

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

Nelson Elhage committed
20
	return 0;
21 22
}

23
int LLVMFuzzerInitialize(int *argc, char ***argv)
24
{
25 26 27 28 29 30 31 32 33 34 35 36
	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;
37
	git_config_backend *backend = NULL;
38 39
	int err = 0;

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

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

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

Nelson Elhage committed
58
	return 0;
59
}