File: inihfuzz.c

package info (click to toggle)
libinih 59-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 484 kB
  • sloc: ansic: 719; cpp: 229; makefile: 68; sh: 62
file content (52 lines) | stat: -rw-r--r-- 1,195 bytes parent folder | download | duplicates (6)
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
/* This is a slightly tweaked copy of tests/unittest.c for fuzzing */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../ini.h"

int User;
char Prev_section[50];

int dumper(void* user, const char* section, const char* name,
           const char* value)
{
    User = *((int*)user);
    if (!name || strcmp(section, Prev_section)) {
        printf("... [%s]\n", section);
        strncpy(Prev_section, section, sizeof(Prev_section));
        Prev_section[sizeof(Prev_section) - 1] = '\0';
    }
    if (!name) {
        return 1;
    }

    printf("... %s%s%s;\n", name, value ? "=" : "", value ? value : "");

    if (!value) {
        // Happens when INI_ALLOW_NO_VALUE=1 and line has no value (no '=' or ':')
        return 1;
    }

    return strcmp(name, "user")==0 && strcmp(value, "parse_error")==0 ? 0 : 1;
}

void parse(const char* fname) {
    static int u = 100;
    int e;

    *Prev_section = '\0';
    e = ini_parse(fname, dumper, &u);
    printf("%s: e=%d user=%d\n", fname, e, User);
    u++;
}

int main(int argc, char **argv)
{
    if (argc < 2) {
        printf("usage: inihfuzz file.ini\n");
        return 1;
    }
    parse(argv[1]);
    return 0;
}