Coverage Report

Created: 2024-05-15 07:10

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