Skip to content

Commit fcdecb8

Browse files
committed
Fuzzing support via AFL
1 parent e492a25 commit fcdecb8

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fuzzing/findings
2+
fuzzing/inihfuzz

fuzzing/build.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
../../afl-2.52b/afl-gcc inihfuzz.c ../ini.c -o inihfuzz

fuzzing/fuzz.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bash
2+
../../afl-2.52b/afl-fuzz -i testcases -o findings -- ./inihfuzz @@

fuzzing/inihfuzz.c

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* This is a slightly tweaked copy of tests/unittest.c for fuzzing */
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include "../ini.h"
7+
8+
int User;
9+
char Prev_section[50];
10+
11+
int dumper(void* user, const char* section, const char* name,
12+
const char* value)
13+
{
14+
User = *((int*)user);
15+
if (!name || strcmp(section, Prev_section)) {
16+
printf("... [%s]\n", section);
17+
strncpy(Prev_section, section, sizeof(Prev_section));
18+
Prev_section[sizeof(Prev_section) - 1] = '\0';
19+
}
20+
if (!name) {
21+
return 1;
22+
}
23+
24+
printf("... %s%s%s;\n", name, value ? "=" : "", value ? value : "");
25+
26+
if (!value) {
27+
// Happens when INI_ALLOW_NO_VALUE=1 and line has no value (no '=' or ':')
28+
return 1;
29+
}
30+
31+
return strcmp(name, "user")==0 && strcmp(value, "parse_error")==0 ? 0 : 1;
32+
}
33+
34+
void parse(const char* fname) {
35+
static int u = 100;
36+
int e;
37+
38+
*Prev_section = '\0';
39+
e = ini_parse(fname, dumper, &u);
40+
printf("%s: e=%d user=%d\n", fname, e, User);
41+
u++;
42+
}
43+
44+
int main(int argc, char **argv)
45+
{
46+
if (argc < 2) {
47+
printf("usage: inihfuzz file.ini\n");
48+
return 1;
49+
}
50+
parse(argv[1]);
51+
return 0;
52+
}

fuzzing/testcases/case1.ini

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; comment
2+
3+
[foo] ; section
4+
bar=1 ; name=value
5+
6+
[bar]
7+
name = Bob
8+
age: 42

0 commit comments

Comments
 (0)