Coverage Report

Created: 2024-04-26 11:08

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