summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2019-05-10 21:16:19 +0100
committerGitHub <noreply@github.com>2019-05-10 21:16:19 +0100
commit34ed40f2e56703de04241cbacb306113b59a84f9 (patch)
treef6fb5006492c0d003af42af15293e02469dd26d7 /Python
parent069a5b48334a795d3abe3a512dd41aad7a532a73 (diff)
downloadcpython-git-34ed40f2e56703de04241cbacb306113b59a84f9.tar.gz
[3.7] bpo-34408: Prevent a null pointer dereference and resource leakage in `PyInterpreterState_New()` (GH-8767) (GH-13237)
* A pointer in `PyInterpreterState_New()` could have been `NULL` when being dereferenced. * Memory was leaked in `PyInterpreterState_New()` when taking some error-handling code path. (cherry picked from commit 95d630e) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/pystate.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index 8077a3ee7a..fc695c62a3 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -167,23 +167,27 @@ PyInterpreterState_New(void)
interp->pyexitmodule = NULL;
HEAD_LOCK();
- interp->next = _PyRuntime.interpreters.head;
- if (_PyRuntime.interpreters.main == NULL) {
- _PyRuntime.interpreters.main = interp;
- }
- _PyRuntime.interpreters.head = interp;
if (_PyRuntime.interpreters.next_id < 0) {
/* overflow or Py_Initialize() not called! */
PyErr_SetString(PyExc_RuntimeError,
"failed to get an interpreter ID");
- /* XXX deallocate! */
+ PyMem_RawFree(interp);
interp = NULL;
} else {
interp->id = _PyRuntime.interpreters.next_id;
_PyRuntime.interpreters.next_id += 1;
+ interp->next = _PyRuntime.interpreters.head;
+ if (_PyRuntime.interpreters.main == NULL) {
+ _PyRuntime.interpreters.main = interp;
+ }
+ _PyRuntime.interpreters.head = interp;
}
HEAD_UNLOCK();
+ if (interp == NULL) {
+ return NULL;
+ }
+
interp->tstate_next_unique_id = 0;
return interp;