Coverage Report

Created: 2025-08-26 06:42

/src/libxml2/fuzz/xpath.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * xpath.c: a libFuzzer target to test XPath and XPointer expressions.
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/xpointer.h>
10
#include "fuzz.h"
11
12
int
13
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
14
20
                     char ***argv ATTRIBUTE_UNUSED) {
15
20
    xmlFuzzMemSetup();
16
20
    xmlInitParser();
17
20
#ifdef LIBXML_CATALOG_ENABLED
18
20
    xmlInitializeCatalog();
19
20
    xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
20
20
#endif
21
20
    xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
22
23
20
    return 0;
24
20
}
25
26
int
27
25.0k
LLVMFuzzerTestOneInput(const char *data, size_t size) {
28
25.0k
    xmlDocPtr doc;
29
25.0k
    const char *expr, *xml;
30
25.0k
    size_t failurePos, exprSize, xmlSize;
31
32
25.0k
    if (size > 10000)
33
1
        return(0);
34
35
25.0k
    xmlFuzzDataInit(data, size);
36
37
25.0k
    failurePos = xmlFuzzReadInt(4) % (size + 100);
38
25.0k
    expr = xmlFuzzReadString(&exprSize);
39
25.0k
    xml = xmlFuzzReadString(&xmlSize);
40
41
    /* Recovery mode allows more input to be fuzzed. */
42
25.0k
    doc = xmlReadMemory(xml, xmlSize, NULL, NULL, XML_PARSE_RECOVER);
43
25.0k
    if (doc != NULL) {
44
24.9k
        xmlXPathContextPtr xpctxt;
45
46
24.9k
        xmlFuzzInjectFailure(failurePos);
47
48
24.9k
        xpctxt = xmlXPathNewContext(doc);
49
24.9k
        if (xpctxt != NULL) {
50
24.8k
            int res;
51
52
            /* Operation limit to avoid timeout */
53
24.8k
            xpctxt->opLimit = 500000;
54
55
24.8k
            res = xmlXPathContextSetCache(xpctxt, 1, 4, 0);
56
24.8k
            xmlFuzzCheckFailureReport("xmlXPathContextSetCache", res == -1, 0);
57
58
24.8k
            xmlFuzzResetFailure();
59
24.8k
            xmlXPathFreeObject(xmlXPtrEval(BAD_CAST expr, xpctxt));
60
24.8k
            xmlFuzzCheckFailureReport("xmlXPtrEval",
61
24.8k
                    xpctxt->lastError.code == XML_ERR_NO_MEMORY, 0);
62
24.8k
            xmlXPathFreeContext(xpctxt);
63
24.8k
        }
64
65
24.9k
        xmlFuzzInjectFailure(0);
66
24.9k
        xmlFreeDoc(doc);
67
24.9k
    }
68
69
25.0k
    xmlFuzzDataCleanup();
70
25.0k
    xmlResetLastError();
71
72
25.0k
    return(0);
73
25.0k
}
74
75
size_t
76
LLVMFuzzerCustomMutator(char *data, size_t size, size_t maxSize,
77
0
                        unsigned seed) {
78
0
    static const xmlFuzzChunkDesc chunks[] = {
79
0
        { 4, XML_FUZZ_PROB_ONE / 10 }, /* failurePos */
80
0
        { 0, 0 }
81
0
    };
82
83
0
    return xmlFuzzMutateChunks(chunks, data, size, maxSize, seed,
84
0
                               LLVMFuzzerMutate);
85
0
}
86