Coverage Report

Created: 2024-05-15 07:10

/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
3.70k
                     char ***argv ATTRIBUTE_UNUSED) {
17
3.70k
    xmlInitParser();
18
3.70k
#ifdef LIBXML_CATALOG_ENABLED
19
3.70k
    xmlInitializeCatalog();
20
3.70k
#endif
21
3.70k
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
22
3.70k
    xmlSetExternalEntityLoader(xmlFuzzEntityLoader);
23
24
3.70k
    return 0;
25
3.70k
}
26
27
int
28
366k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
29
366k
    static const size_t maxChunkSize = 128;
30
366k
    xmlDocPtr doc;
31
366k
    xmlParserCtxtPtr ctxt;
32
366k
    xmlTextReaderPtr reader;
33
366k
    xmlChar *out;
34
366k
    const char *docBuffer, *docUrl;
35
366k
    size_t docSize, consumed, chunkSize;
36
366k
    int opts, outSize;
37
38
366k
    xmlFuzzDataInit(data, size);
39
366k
    opts = xmlFuzzReadInt();
40
366k
    opts &= ~XML_PARSE_XINCLUDE;
41
42
366k
    xmlFuzzReadEntities();
43
366k
    docBuffer = xmlFuzzMainEntity(&docSize);
44
366k
    docUrl = xmlFuzzMainUrl();
45
366k
    if (docBuffer == NULL)
46
153
        goto exit;
47
48
    /* Pull parser */
49
50
365k
    doc = xmlReadMemory(docBuffer, docSize, docUrl, NULL, opts);
51
    /* Also test the serializer. */
52
365k
    xmlDocDumpMemory(doc, &out, &outSize);
53
365k
    xmlFree(out);
54
365k
    xmlFreeDoc(doc);
55
56
    /* Push parser */
57
58
365k
    ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
59
365k
    if (ctxt == NULL)
60
0
        goto exit;
61
365k
    xmlCtxtUseOptions(ctxt, opts);
62
63
7.84M
    for (consumed = 0; consumed < docSize; consumed += chunkSize) {
64
7.47M
        chunkSize = docSize - consumed;
65
7.47M
        if (chunkSize > maxChunkSize)
66
7.11M
            chunkSize = maxChunkSize;
67
7.47M
        xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0);
68
7.47M
    }
69
70
365k
    xmlParseChunk(ctxt, NULL, 0, 1);
71
365k
    xmlFreeDoc(ctxt->myDoc);
72
365k
    xmlFreeParserCtxt(ctxt);
73
74
    /* Reader */
75
76
365k
    reader = xmlReaderForMemory(docBuffer, docSize, NULL, NULL, opts);
77
365k
    if (reader == NULL)
78
0
        goto exit;
79
6.29M
    while (xmlTextReaderRead(reader) == 1) {
80
5.92M
        if (xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE) {
81
2.09M
            int i, n = xmlTextReaderAttributeCount(reader);
82
5.16M
            for (i=0; i<n; i++) {
83
3.06M
                xmlTextReaderMoveToAttributeNo(reader, i);
84
6.14M
                while (xmlTextReaderReadAttributeValue(reader) == 1);
85
3.06M
            }
86
2.09M
        }
87
5.92M
    }
88
365k
    xmlFreeTextReader(reader);
89
90
366k
exit:
91
366k
    xmlFuzzDataCleanup();
92
366k
    xmlResetLastError();
93
366k
    return(0);
94
365k
}
95