Skip to content

Commit f775af3

Browse files
bpo-22689: Copy the result of getenv() in sys_breakpointhook().
1 parent 2a9b8ba commit f775af3

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Python/sysmodule.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static PyObject *
107107
sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *keywords)
108108
{
109109
assert(!PyErr_Occurred());
110-
const char *envar = Py_GETENV("PYTHONBREAKPOINT");
110+
char *envar = Py_GETENV("PYTHONBREAKPOINT");
111111

112112
if (envar == NULL || strlen(envar) == 0) {
113113
envar = "pdb.set_trace";
@@ -116,6 +116,11 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
116116
/* The breakpoint is explicitly no-op'd. */
117117
Py_RETURN_NONE;
118118
}
119+
envar = _PyMem_RawStrdup(envar);
120+
if (envar == NULL) {
121+
PyErr_NoMemory();
122+
return NULL;
123+
}
119124
const char *last_dot = strrchr(envar, '.');
120125
const char *attrname = NULL;
121126
PyObject *modulepath = NULL;
@@ -131,12 +136,14 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
131136
attrname = last_dot + 1;
132137
}
133138
if (modulepath == NULL) {
139+
PyMem_RawFree(envar);
134140
return NULL;
135141
}
136142

137143
PyObject *fromlist = Py_BuildValue("(s)", attrname);
138144
if (fromlist == NULL) {
139145
Py_DECREF(modulepath);
146+
PyMem_RawFree(envar);
140147
return NULL;
141148
}
142149
PyObject *module = PyImport_ImportModuleLevelObject(
@@ -145,11 +152,13 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
145152
Py_DECREF(fromlist);
146153

147154
if (module == NULL) {
155+
PyMem_RawFree(envar);
148156
goto error;
149157
}
150158

151159
PyObject *hook = PyObject_GetAttrString(module, attrname);
152160
Py_DECREF(module);
161+
PyMem_RawFree(envar);
153162

154163
if (hook == NULL) {
155164
goto error;

0 commit comments

Comments
 (0)