/src/mercurial/contrib/fuzz/dirs.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 "pyutil.h" |
7 | | |
8 | | #include <string> |
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 | try: |
19 | 16 | files = mdata.split('\n') |
20 | 16 | d = parsers.dirs(files) |
21 | 16 | list(d) |
22 | 16 | 'a' in d |
23 | 16 | if files: |
24 | 16 | files[0] in d |
25 | 16 | except Exception as e: |
26 | 16 | pass |
27 | 16 | # uncomment this print if you're editing this Python code |
28 | 16 | # to debug failures. |
29 | 16 | # print e |
30 | 16 | )py", |
31 | 16 | "fuzzer", Py_file_input); |
32 | 16 | return 0; |
33 | 16 | } |
34 | | |
35 | | int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
36 | 164 | { |
37 | | // Don't allow fuzzer inputs larger than 100k, since we'll just bog |
38 | | // down and not accomplish much. |
39 | 164 | if (Size > 100000) { |
40 | 9 | return 0; |
41 | 9 | } |
42 | 155 | PyObject *mtext = |
43 | 155 | PyBytes_FromStringAndSize((const char *)Data, (Py_ssize_t)Size); |
44 | 155 | PyObject *locals = PyDict_New(); |
45 | 155 | PyDict_SetItemString(locals, "mdata", mtext); |
46 | 155 | PyObject *res = PyEval_EvalCode(code, contrib::pyglobals(), locals); |
47 | 155 | if (!res) { |
48 | 0 | PyErr_Print(); |
49 | 0 | } |
50 | 155 | Py_XDECREF(res); |
51 | 155 | Py_DECREF(locals); |
52 | 155 | Py_DECREF(mtext); |
53 | 155 | return 0; // Non-zero return values are reserved for future use. |
54 | 164 | } |
55 | | } |