]> wimlib.net Git - wimlib/blob - tools/libFuzzer/xmlproc/fuzz.c
libFuzzer: add xml_windows fuzzer
[wimlib] / tools / libFuzzer / xmlproc / fuzz.c
1 #include "../fuzzer.h"
2
3 /* Fuzz XML parsing and writing. */
4 int LLVMFuzzerTestOneInput(const uint8_t *in, size_t insize)
5 {
6         uint16_t fault_nth;
7         char *in_str;
8         char *out_str = NULL;
9         int ret;
10
11         if (!setup_fault_nth(&in, &insize, &fault_nth))
12                 return 0;
13
14         in_str = malloc(insize + 1);
15         memcpy(in_str, in, insize);
16         in_str[insize] = '\0';
17         ret = wimlib_parse_and_write_xml_doc(in_str, &out_str);
18         if (ret == 0) {
19                 char *out2_str = NULL;
20
21                 /*
22                  * If the first parse+write succeeded, we now should be able to
23                  * parse+write the result without changing it further.
24                  */
25                 ret = wimlib_parse_and_write_xml_doc(out_str, &out2_str);
26                 if (ret != 0)
27                         assert(ret == WIMLIB_ERR_NOMEM && fault_nth);
28                 else
29                         assert(strcmp(out_str, out2_str) == 0);
30                 free(out2_str);
31         } else {
32                 assert(ret == WIMLIB_ERR_XML ||
33                        (fault_nth && ret == WIMLIB_ERR_NOMEM));
34         }
35         free(in_str);
36         free(out_str);
37         return 0;
38 }