/src/mercurial/contrib/fuzz/fm1readmarkers.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <Python.h> |
2 | | #include <assert.h> |
3 | | #include <stdlib.h> |
4 | | #include <unistd.h> |
5 | | |
6 | | #include <string> |
7 | | |
8 | | #include "pyutil.h" |
9 | | |
10 | | extern "C" { |
11 | | |
12 | | static PYCODETYPE *code; |
13 | | |
14 | | extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) |
15 | 16 | { |
16 | 16 | contrib::initpy(*argv[0]); |
17 | 16 | code = (PYCODETYPE *)Py_CompileString(R"py( |
18 | 16 | def maybeint(s, default): |
19 | 16 | try: |
20 | 16 | return int(s) |
21 | 16 | except ValueError: |
22 | 16 | return default |
23 | 16 | try: |
24 | 16 | parts = data.split('\0', 2) |
25 | 16 | if len(parts) == 3: |
26 | 16 | offset, stop, data = parts |
27 | 16 | elif len(parts) == 2: |
28 | 16 | stop, data = parts |
29 | 16 | offset = 0 |
30 | 16 | else: |
31 | 16 | offset = stop = 0 |
32 | 16 | offset, stop = maybeint(offset, 0), maybeint(stop, len(data)) |
33 | 16 | parsers.fm1readmarkers(data, offset, stop) |
34 | 16 | except Exception as e: |
35 | 16 | pass |
36 | 16 | # uncomment this print if you're editing this Python code |
37 | 16 | # to debug failures. |
38 | 16 | # print e |
39 | 16 | )py", |
40 | 16 | "fuzzer", Py_file_input); |
41 | 16 | return 0; |
42 | 16 | } |
43 | | |
44 | | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
45 | 1.84k | { |
46 | 1.84k | PyObject *text = |
47 | 1.84k | PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); |
48 | 1.84k | PyObject *locals = PyDict_New(); |
49 | 1.84k | PyDict_SetItemString(locals, "data", text); |
50 | 1.84k | PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
51 | 1.84k | if (!res) { |
52 | 0 | PyErr_Print(); |
53 | 0 | } |
54 | 1.84k | Py_XDECREF(res); |
55 | 1.84k | Py_DECREF(locals); |
56 | 1.84k | Py_DECREF(text); |
57 | 1.84k | return 0; // Non-zero return values are reserved for future use. |
58 | 1.84k | } |
59 | | } |