Coverage Report

Created: 2024-08-17 10:59

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