summaryrefslogtreecommitdiffstats
path: root/tests/fuzz/test-fuzz.c
blob: 9946a5786ce1574a907336c794a3ab0676967251 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>

#include "uci.h"

static void fuzz_uci_import(const uint8_t *input, size_t size)
{
	int r;
	int fd;
	FILE *fs = NULL;
	struct uci_context *ctx = NULL;
	struct uci_package *package = NULL;

	fd = open("/dev/shm", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
	if (fd < 0) {
		perror("unable to create temp file");
		exit(-1);
	}

	r = write(fd, input, size);
	if (r < 0) {
		perror("unable to write()");
		exit(-1);
	}

	fs = fdopen(fd, "r");
	if (fs == NULL) {
		perror("unable to fdopen()");
		exit(-1);
	}

	fseek(fs, 0L, SEEK_SET);

	ctx = uci_alloc_context();
	if (ctx == NULL) {
		perror("unable to uci_alloc_context()");
		exit(-1);
	}

	uci_import(ctx, fs, NULL, &package, false);
	uci_free_context(ctx);
	close(fd);
	fclose(fs);
}

int LLVMFuzzerTestOneInput(const uint8_t *input, size_t size)
{
	fuzz_uci_import(input, size);
	return 0;
}