config_file_fuzzer.c 1.27 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 11 12 13 14 15
#include <git2.h>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
16
#include <errno.h>
17

18 19
#define UNUSED(x) (void)(x)

Nelson Elhage committed
20 21
int foreach_cb(const git_config_entry *entry, void *payload)
{
22 23 24
	UNUSED(entry);
	UNUSED(payload);

Nelson Elhage committed
25
	return 0;
26 27
}

28 29 30 31
static char path[] = "/tmp/git.XXXXXX";
static int fd = -1;

int LLVMFuzzerInitialize(int *argc, char ***argv)
32
{
33 34 35 36 37 38
	UNUSED(argc);
	UNUSED(argv);

	if (git_libgit2_init() < 0)
		abort();
	fd = mkstemp(path);
Nelson Elhage committed
39
	if (fd < 0) {
40
		abort();
Nelson Elhage committed
41
	}
42 43 44 45 46 47 48 49

	return 0;
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
	git_config *cfg = NULL;
	int err = 0;
50
	size_t total = 0;
51

Nelson Elhage committed
52 53 54 55 56 57
	if (ftruncate(fd, 0) !=0 ) {
		abort();
	}
	if (lseek(fd, 0, SEEK_SET) != 0) {
		abort();
	}
58 59 60 61 62 63 64 65

	while (total < size) {
		ssize_t written = write(fd, data, size);
		if (written < 0 && errno != EINTR)
			abort();
		if (written < 0)
			continue;
		total += written;
Nelson Elhage committed
66
	}
67

68
	err = git_config_open_ondisk(&cfg, path);
Nelson Elhage committed
69 70 71 72
	if (err == 0) {
		git_config_foreach(cfg, foreach_cb, NULL);
		git_config_free(cfg);
	}
73

Nelson Elhage committed
74
	return 0;
75
}