Coverage Report

Created: 2026-06-09 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mercurial/contrib/fuzz/fncache.cc
Line
Count
Source
1
#include <Python.h>
2
#include <assert.h>
3
#include <stdlib.h>
4
#include <unistd.h>
5
6
#include "pyutil.h"
7
8
#include <iostream>
9
#include <string>
10
11
extern "C" {
12
13
static PYCODETYPE *code;
14
15
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
16
3
{
17
3
  contrib::initpy(*argv[0]);
18
3
  code = (PYCODETYPE *)Py_CompileString(R"py(
19
3
try:
20
3
    for fn in (
21
3
        parsers.isasciistr,
22
3
        parsers.asciilower,
23
3
        parsers.asciiupper,
24
3
        parsers.encodedir,
25
3
        parsers.pathencode,
26
3
        parsers.lowerencode,
27
3
    ):
28
3
        try:
29
3
            fn(data)
30
3
        except UnicodeDecodeError:
31
3
            pass  # some functions emit this exception
32
3
        except AttributeError:
33
3
            # pathencode needs hashlib, which fails to import because the time
34
3
            # module fails to import. We should try and fix that some day, but
35
3
            # for now we at least get coverage on non-hashencoded codepaths.
36
3
            if fn != pathencode:
37
3
                raise
38
3
        # uncomment this for debugging exceptions
39
3
        # except Exception as e:
40
3
        #     raise Exception('%r: %r' % (fn, e))
41
3
except Exception as e:
42
3
    pass
43
3
    # uncomment this print if you're editing this Python code
44
3
    # to debug failures.
45
3
    # print(e)
46
3
)py",
47
3
                                        "fuzzer", Py_file_input);
48
3
  if (!code) {
49
0
    std::cerr << "failed to compile Python code!" << std::endl;
50
0
  }
51
3
  return 0;
52
3
}
53
54
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
55
2.15k
{
56
2.15k
  PyObject *mtext =
57
2.15k
      PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size);
58
2.15k
  PyObject *locals = PyDict_New();
59
2.15k
  PyDict_SetItemString(locals, "data", mtext);
60
2.15k
  PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals);
61
2.15k
  if (!res) {
62
0
    PyErr_Print();
63
0
  }
64
2.15k
  Py_XDECREF(res);
65
2.15k
  Py_DECREF(locals);
66
2.15k
  Py_DECREF(mtext);
67
2.15k
  return 0; // Non-zero return values are reserved for future use.
68
2.15k
}
69
}