/src/pupnp/fuzzer/FuzzIxml.c
Line | Count | Source |
1 | | #include "ixml.h" |
2 | | #include <stdint.h> |
3 | | #include <stdio.h> |
4 | | #include <stdlib.h> |
5 | | #include <string.h> |
6 | | #include <unistd.h> |
7 | | |
8 | 7.34k | #define kMinInputLength 10 |
9 | 3.66k | #define kMaxInputLength 5120 |
10 | | |
11 | | int CheckXML(char *filename) |
12 | 3.64k | { |
13 | | |
14 | 3.64k | int rc; |
15 | 3.64k | DOMString s; |
16 | 3.64k | IXML_Document *doc = NULL; |
17 | | |
18 | 3.64k | rc = ixmlLoadDocumentEx(filename, &doc); |
19 | 3.64k | if (rc != IXML_SUCCESS) { |
20 | 3.34k | return rc; |
21 | 3.34k | } |
22 | | |
23 | 307 | s = ixmlPrintDocument(doc); |
24 | 307 | if (s == NULL || s[0] == '\0') { |
25 | 0 | ixmlDocument_free(doc); |
26 | 0 | return 1; |
27 | 0 | } |
28 | | |
29 | 307 | ixmlFreeDOMString(s); |
30 | 307 | ixmlDocument_free(doc); |
31 | | |
32 | 307 | return 0; |
33 | 307 | } |
34 | | |
35 | | extern int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
36 | 3.67k | { |
37 | 3.67k | char filename[256]; |
38 | 3.67k | FILE *fp; |
39 | | |
40 | 3.67k | if (Size < kMinInputLength || Size > kMaxInputLength) { |
41 | 25 | return 0; |
42 | 25 | } |
43 | | |
44 | 3.64k | snprintf(filename, sizeof(filename), "/tmp/libfuzzer.%d", getpid()); |
45 | 3.64k | fp = fopen(filename, "wb"); |
46 | 3.64k | if (!fp) { |
47 | 0 | return 0; |
48 | 0 | } |
49 | | |
50 | 3.64k | fwrite(Data, Size, 1, fp); |
51 | 3.64k | fclose(fp); |
52 | | |
53 | | /* libFuzzer only accepts 0 and -1, and leaves an input out of the |
54 | | * corpus on anything but 0: a document that fails to parse must |
55 | | * still return 0. */ |
56 | 3.64k | (void)CheckXML(filename); |
57 | 3.64k | unlink(filename); |
58 | 3.64k | return 0; |
59 | 3.64k | } |