Coverage Report

Created: 2024-01-17 17:01

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