[3.13] gh-130163: Fix crashes related to PySys_GetObject() (GH-130503) (GH-130556)

The use of PySys_GetObject() and _PySys_GetAttr(), which return a borrowed
reference, has been replaced by using one of the following functions, which
return a strong reference and distinguish a missing attribute from an error:
_PySys_GetOptionalAttr(), _PySys_GetOptionalAttrString(),
_PySys_GetRequiredAttr(), and _PySys_GetRequiredAttrString().
(cherry picked from commit 0ef4ffeefd)
This commit is contained in:
Serhiy Storchaka 2025-02-26 00:50:26 +02:00 committed by GitHub
parent b0d3f49195
commit 7c1b76fce8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 505 additions and 180 deletions

View file

@ -5,7 +5,7 @@
#include "pycore_pyerrors.h" // _PyErr_Occurred()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_sysmodule.h" // _PySys_GetAttr()
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttr()
#include "pycore_traceback.h" // _Py_DisplaySourceLine()
#include <stdbool.h>
@ -494,7 +494,7 @@ static void
show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
PyObject *text, PyObject *category, PyObject *sourceline)
{
PyObject *f_stderr;
PyObject *f_stderr = NULL;
PyObject *name;
char lineno_str[128];
@ -505,8 +505,7 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
goto error;
}
f_stderr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
if (f_stderr == NULL) {
if (_PySys_GetOptionalAttr(&_Py_ID(stderr), &f_stderr) <= 0) {
fprintf(stderr, "lost sys.stderr\n");
goto error;
}
@ -556,6 +555,7 @@ show_warning(PyThreadState *tstate, PyObject *filename, int lineno,
}
error:
Py_XDECREF(f_stderr);
Py_XDECREF(name);
PyErr_Clear();
}