Coverage Report

Created: 2024-08-16 03:22

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