File: fuzz_mmdb.c

package info (click to toggle)
python-maxminddb 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,600 kB
  • sloc: ansic: 7,565; python: 1,711; perl: 987; makefile: 273; sh: 190
file content (34 lines) | stat: -rw-r--r-- 753 bytes parent folder | download | duplicates (2)
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
#include "maxminddb-compat-util.h"
#include "maxminddb.h"
#include <unistd.h>

#define kMinInputLength 2
#define kMaxInputLength 4048

extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
    int status;
    FILE *fp;
    MMDB_s mmdb;
    char filename[256];

    if (size < kMinInputLength || size > kMaxInputLength)
        return 0;

    sprintf(filename, "/tmp/libfuzzer.%d", getpid());

    fp = fopen(filename, "wb");
    if (!fp)
        return 0;

    fwrite(data, size, sizeof(uint8_t), fp);
    fclose(fp);

    status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb);
    if (status == MMDB_SUCCESS)
        MMDB_close(&mmdb);

    unlink(filename);
    return 0;
}