Coverage Report

Created: 2024-01-18 09:16

/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/xinclude.h>
12
#include <libxml/xmlreader.h>
13
#include "fuzz.h"
14
15
int
16
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
17
1.34k
                     char ***argv ATTRIBUTE_UNUSED) {
18
1.34k
    xmlInitParser();
19
1.34k
#ifdef LIBXML_CATALOG_ENABLED
20
1.34k
    xmlInitializeCatalog();
21
1.34k
#endif
22
1.34k
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
23
1.34k
    xmlSetExternalEntityLoader(xmlFuzzEntityLoader);
24
25
1.34k
    return 0;
26
1.34k
}
27
28
int
29
112k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
30
112k
    static const size_t maxChunkSize = 128;
31
112k
    xmlDocPtr doc;
32
112k
    xmlParserCtxtPtr ctxt;
33
112k
    xmlTextReaderPtr reader;
34
112k
    xmlChar *out;
35
112k
    const char *docBuffer, *docUrl;
36
112k
    size_t maxSize, docSize, consumed, chunkSize;
37
112k
    int opts, outSize;
38
39
112k
    xmlFuzzDataInit(data, size);
40
112k
    opts = xmlFuzzReadInt();
41
42
    /* Lower maximum size when processing entities for now. */
43
112k
    maxSize = opts & XML_PARSE_NOENT ? 50000 : 500000;
44
112k
    if (size > maxSize)
45
43
        goto exit;
46
47
112k
    xmlFuzzReadEntities();
48
112k
    docBuffer = xmlFuzzMainEntity(&docSize);
49
112k
    docUrl = xmlFuzzMainUrl();
50
112k
    if (docBuffer == NULL)
51
37
        goto exit;
52
53
    /* Pull parser */
54
55
112k
    doc = xmlReadMemory(docBuffer, docSize, docUrl, NULL, opts);
56
112k
    if (opts & XML_PARSE_XINCLUDE)
57
53.7k
        xmlXIncludeProcessFlags(doc, opts);
58
    /* Also test the serializer. */
59
112k
    xmlDocDumpMemory(doc, &out, &outSize);
60
112k
    xmlFree(out);
61
112k
    xmlFreeDoc(doc);
62
63
    /* Push parser */
64
65
112k
    ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, docUrl);
66
112k
    if (ctxt == NULL)
67
0
        goto exit;
68
112k
    xmlCtxtUseOptions(ctxt, opts);
69
70
1.52M
    for (consumed = 0; consumed < docSize; consumed += chunkSize) {
71
1.41M
        chunkSize = docSize - consumed;
72
1.41M
        if (chunkSize > maxChunkSize)
73
1.30M
            chunkSize = maxChunkSize;
74
1.41M
        xmlParseChunk(ctxt, docBuffer + consumed, chunkSize, 0);
75
1.41M
    }
76
77
112k
    xmlParseChunk(ctxt, NULL, 0, 1);
78
112k
    if (opts & XML_PARSE_XINCLUDE)
79
53.7k
        xmlXIncludeProcessFlags(ctxt->myDoc, opts);
80
112k
    xmlFreeDoc(ctxt->myDoc);
81
112k
    xmlFreeParserCtxt(ctxt);
82
83
    /* Reader */
84
85
112k
    reader = xmlReaderForMemory(docBuffer, docSize, NULL, NULL, opts);
86
112k
    if (reader == NULL)
87
0
        goto exit;
88
453k
    while (xmlTextReaderRead(reader) == 1) {
89
340k
        if (xmlTextReaderNodeType(reader) == XML_ELEMENT_NODE) {
90
129k
            int i, n = xmlTextReaderAttributeCount(reader);
91
197k
            for (i=0; i<n; i++) {
92
68.6k
                xmlTextReaderMoveToAttributeNo(reader, i);
93
139k
                while (xmlTextReaderReadAttributeValue(reader) == 1);
94
68.6k
            }
95
129k
        }
96
340k
    }
97
112k
    xmlFreeTextReader(reader);
98
99
112k
exit:
100
112k
    xmlFuzzDataCleanup();
101
112k
    xmlResetLastError();
102
112k
    return(0);
103
112k
}
104