##// END OF EJS Templates
fuzz: fix an unused result on getcwd() in pyutil...
Augie Fackler -
r44259:c78f8f07 default
parent child Browse files
Show More
@@ -1,49 +1,54 b''
1 #include "pyutil.h"
1 #include "pyutil.h"
2
2
3 #include <iostream>
3 #include <string>
4 #include <string>
4
5
5 namespace contrib
6 namespace contrib
6 {
7 {
7
8
8 static char cpypath[8192] = "\0";
9 static char cpypath[8192] = "\0";
9
10
10 static PyObject *mainmod;
11 static PyObject *mainmod;
11 static PyObject *globals;
12 static PyObject *globals;
12
13
13 /* TODO: use Python 3 for this fuzzing? */
14 /* TODO: use Python 3 for this fuzzing? */
14 PyMODINIT_FUNC initparsers(void);
15 PyMODINIT_FUNC initparsers(void);
15
16
16 void initpy(const char *cselfpath)
17 void initpy(const char *cselfpath)
17 {
18 {
18 const std::string subdir = "/sanpy/lib/python2.7";
19 const std::string subdir = "/sanpy/lib/python2.7";
19 /* HACK ALERT: we need a full Python installation built without
20 /* HACK ALERT: we need a full Python installation built without
20 pymalloc and with ASAN, so we dump one in
21 pymalloc and with ASAN, so we dump one in
21 $OUT/sanpy/lib/python2.7. This helps us wire that up. */
22 $OUT/sanpy/lib/python2.7. This helps us wire that up. */
22 std::string selfpath(cselfpath);
23 std::string selfpath(cselfpath);
23 std::string pypath;
24 std::string pypath;
24 auto pos = selfpath.rfind("/");
25 auto pos = selfpath.rfind("/");
25 if (pos == std::string::npos) {
26 if (pos == std::string::npos) {
26 char wd[8192];
27 char wd[8192];
27 getcwd(wd, 8192);
28 if (!getcwd(wd, 8192)) {
29 std::cerr << "Failed to call getcwd: errno " << errno
30 << std::endl;
31 exit(1);
32 }
28 pypath = std::string(wd) + subdir;
33 pypath = std::string(wd) + subdir;
29 } else {
34 } else {
30 pypath = selfpath.substr(0, pos) + subdir;
35 pypath = selfpath.substr(0, pos) + subdir;
31 }
36 }
32 strncpy(cpypath, pypath.c_str(), pypath.size());
37 strncpy(cpypath, pypath.c_str(), pypath.size());
33 setenv("PYTHONPATH", cpypath, 1);
38 setenv("PYTHONPATH", cpypath, 1);
34 setenv("PYTHONNOUSERSITE", "1", 1);
39 setenv("PYTHONNOUSERSITE", "1", 1);
35 /* prevent Python from looking up users in the fuzz environment */
40 /* prevent Python from looking up users in the fuzz environment */
36 setenv("PYTHONUSERBASE", cpypath, 1);
41 setenv("PYTHONUSERBASE", cpypath, 1);
37 Py_SetPythonHome(cpypath);
42 Py_SetPythonHome(cpypath);
38 Py_InitializeEx(0);
43 Py_InitializeEx(0);
39 mainmod = PyImport_AddModule("__main__");
44 mainmod = PyImport_AddModule("__main__");
40 globals = PyModule_GetDict(mainmod);
45 globals = PyModule_GetDict(mainmod);
41 initparsers();
46 initparsers();
42 }
47 }
43
48
44 PyObject *pyglobals()
49 PyObject *pyglobals()
45 {
50 {
46 return globals;
51 return globals;
47 }
52 }
48
53
49 } // namespace contrib
54 } // namespace contrib
General Comments 0
You need to be logged in to leave comments. Login now