Coverage Report

Created: 2023-04-29 07:37

/src/libxml2/fuzz/xml.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * xml.c: a libFuzzer target to test several XML parser interfaces.
3
 *
4
 * See Copyright for the status of this software.
5
 */
6
7
#include <libxml/catalog.h>
8
#include <libxml/parser.h>
9
#include <libxml/tree.h>
10
#include <libxml/xmlerror.h>
11
#include <libxml/xmlreader.h>
12
#include "fuzz.h"
13
14
int
15
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
16
98
                     char ***argv ATTRIBUTE_UNUSED) {
17
98
    xmlInitParser();
18
98
#ifdef LIBXML_CATALOG_ENABLED
19
98
    xmlInitializeCatalog();
20
98
#endif
21
98
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
22
98
    xmlSetExternalEntityLoader(xmlFuzzEntityLoader);
23
24
98
    return 0;
25
98
}
26
27
int
28
240k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
29
240k
    static const size_t maxChunkSize = 128;
30
240k
    xmlDocPtr doc;
31
240k
    xmlParserCtxtPtr ctxt;
32
240k
    xmlTextReaderPtr reader;
33
240k
    xmlChar *out;
34
240k
    const char *docBuffer, *docUrl;
35
240k
    size_t docSize, consumed, chunkSize;
36
240k
    int opts, outSize;
37
38
240k
    xmlFuzzDataInit(data, size);
39
240k
    opts = xmlFuzzReadInt();
40
240k
    opts &= ~XML_PARSE_XINCLUDE;
41
42
240k
    xmlFuzzReadEntities();
43
240k
    docBuffer = xmlFuzzMainEntity(&docSize);
44
240k
    docUrl = xmlFuzzMainUrl();
45
240k
    if (docBuffer == NULL)
46
12.2k
        goto exit;
47
48
    /* Pull parser */
49
50
228k
    doc = xmlReadMemory(docBuffer, docSize, docUrl, NULL, opts);
51
    /* Also test the serializer. */
52
228k
    xmlDocDumpMemory(doc, &out, &outSize);
53
228k
    xmlFree(out);
54
228k
    xmlFreeDoc(doc);
55
56
    /* Push parser */
57
58
228k
    ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
59
228k
    if (ctxt == NULL)
60
0
        goto exit;
61
228k
    xmlCtxtUseOptions(ctxt, opts);
62
63
1.52M
    for (consumed = 0; consumed < docSize; consumed += chunkSize) {
64
1.29M
        chunkSize = docSize - consumed;
65
1.29M
        if (chunkSize > maxChunkSize)
66
1.06M
            chunkSize = maxChunkSize;
67
1.29M
        xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0);
68
1.29M
    }
69
70
228k
    xmlParseChunk(ctxt, NULL, 0, 1);
71
228k
    xmlFreeDoc(ctxt->myDoc);
72
228k
    xmlFreeParserCtxt(ctxt);
73
74
    /* Reader */
75
76
228k
    reader = xmlReaderForMemory(docBuffer, docSize, NULL, NULL, opts);
77
228k
    if (reader == NULL)
78
0
        goto exit;
79
244k
    while (xmlTextReaderRead(reader) == 1) {
80
15.4k
        if (xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE) {
81
14.9k
            int i, n = xmlTextReaderAttributeCount(reader);
82
15.2k
            for (i=0; i<n; i++) {
83
307
                xmlTextReaderMoveToAttributeNo(reader, i);
84
589
                while (xmlTextReaderReadAttributeValue(reader) == 1);
85
307
            }
86
14.9k
        }
87
15.4k
    }
88
228k
    xmlFreeTextReader(reader);
89
90
240k
exit:
91
240k
    xmlFuzzDataCleanup();
92
240k
    xmlResetLastError();
93
240k
    return(0);
94
228k
}
95