@@ -107,7 +107,7 @@ static PyObject *
107
107
sys_breakpointhook (PyObject * self , PyObject * const * args , Py_ssize_t nargs , PyObject * keywords )
108
108
{
109
109
assert (!PyErr_Occurred ());
110
- const char * envar = Py_GETENV ("PYTHONBREAKPOINT" );
110
+ char * envar = Py_GETENV ("PYTHONBREAKPOINT" );
111
111
112
112
if (envar == NULL || strlen (envar ) == 0 ) {
113
113
envar = "pdb.set_trace" ;
@@ -116,6 +116,15 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
116
116
/* The breakpoint is explicitly no-op'd. */
117
117
Py_RETURN_NONE ;
118
118
}
119
+ /* According to POSIX the string returned by getenv() might be invalidated
120
+ * or the string content might be overwritten by a subsequent call to
121
+ * getenv(). Since importing a module can performs the getenv() calls,
122
+ * we need to save a copy of envar. */
123
+ envar = _PyMem_RawStrdup (envar );
124
+ if (envar == NULL ) {
125
+ PyErr_NoMemory ();
126
+ return NULL ;
127
+ }
119
128
const char * last_dot = strrchr (envar , '.' );
120
129
const char * attrname = NULL ;
121
130
PyObject * modulepath = NULL ;
@@ -131,12 +140,14 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
131
140
attrname = last_dot + 1 ;
132
141
}
133
142
if (modulepath == NULL ) {
143
+ PyMem_RawFree (envar );
134
144
return NULL ;
135
145
}
136
146
137
147
PyObject * fromlist = Py_BuildValue ("(s)" , attrname );
138
148
if (fromlist == NULL ) {
139
149
Py_DECREF (modulepath );
150
+ PyMem_RawFree (envar );
140
151
return NULL ;
141
152
}
142
153
PyObject * module = PyImport_ImportModuleLevelObject (
@@ -154,6 +165,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
154
165
if (hook == NULL ) {
155
166
goto error ;
156
167
}
168
+ PyMem_RawFree (envar );
157
169
PyObject * retval = _PyObject_FastCallKeywords (hook , args , nargs , keywords );
158
170
Py_DECREF (hook );
159
171
return retval ;
@@ -164,6 +176,7 @@ sys_breakpointhook(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyOb
164
176
int status = PyErr_WarnFormat (
165
177
PyExc_RuntimeWarning , 0 ,
166
178
"Ignoring unimportable $PYTHONBREAKPOINT: \"%s\"" , envar );
179
+ PyMem_RawFree (envar );
167
180
if (status < 0 ) {
168
181
/* Printing the warning raised an exception. */
169
182
return NULL ;
0 commit comments