Coverage Report

Created: 2023-09-29 17:39

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