mirror of
https://github.com/python/cpython.git
synced 2025-08-27 12:16:04 +00:00
[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:
parent
b0d3f49195
commit
7c1b76fce8
23 changed files with 505 additions and 180 deletions
|
@ -1300,8 +1300,12 @@ init_interp_main(PyThreadState *tstate)
|
|||
|
||||
if (is_main_interp) {
|
||||
/* Initialize warnings. */
|
||||
PyObject *warnoptions = PySys_GetObject("warnoptions");
|
||||
if (warnoptions != NULL && PyList_Size(warnoptions) > 0)
|
||||
PyObject *warnoptions;
|
||||
if (_PySys_GetOptionalAttrString("warnoptions", &warnoptions) < 0) {
|
||||
return _PyStatus_ERR("can't initialize warnings");
|
||||
}
|
||||
if (warnoptions != NULL && PyList_Check(warnoptions) &&
|
||||
PyList_Size(warnoptions) > 0)
|
||||
{
|
||||
PyObject *warnings_module = PyImport_ImportModule("warnings");
|
||||
if (warnings_module == NULL) {
|
||||
|
@ -1310,6 +1314,7 @@ init_interp_main(PyThreadState *tstate)
|
|||
}
|
||||
Py_XDECREF(warnings_module);
|
||||
}
|
||||
Py_XDECREF(warnoptions);
|
||||
|
||||
interp->runtime->initialized = 1;
|
||||
}
|
||||
|
@ -1820,24 +1825,33 @@ file_is_closed(PyObject *fobj)
|
|||
static int
|
||||
flush_std_files(void)
|
||||
{
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
PyObject *fout = _PySys_GetAttr(tstate, &_Py_ID(stdout));
|
||||
PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
||||
PyObject *file;
|
||||
int status = 0;
|
||||
|
||||
if (fout != NULL && fout != Py_None && !file_is_closed(fout)) {
|
||||
if (_PyFile_Flush(fout) < 0) {
|
||||
PyErr_FormatUnraisable("Exception ignored on flushing sys.stdout");
|
||||
if (_PySys_GetOptionalAttr(&_Py_ID(stdout), &file) < 0) {
|
||||
status = -1;
|
||||
}
|
||||
else if (file != NULL && file != Py_None && !file_is_closed(file)) {
|
||||
if (_PyFile_Flush(file) < 0) {
|
||||
status = -1;
|
||||
}
|
||||
}
|
||||
if (status < 0) {
|
||||
PyErr_FormatUnraisable("Exception ignored on flushing sys.stdout");
|
||||
}
|
||||
Py_XDECREF(file);
|
||||
|
||||
if (ferr != NULL && ferr != Py_None && !file_is_closed(ferr)) {
|
||||
if (_PyFile_Flush(ferr) < 0) {
|
||||
if (_PySys_GetOptionalAttr(&_Py_ID(stderr), &file) < 0) {
|
||||
PyErr_Clear();
|
||||
status = -1;
|
||||
}
|
||||
else if (file != NULL && file != Py_None && !file_is_closed(file)) {
|
||||
if (_PyFile_Flush(file) < 0) {
|
||||
PyErr_Clear();
|
||||
status = -1;
|
||||
}
|
||||
}
|
||||
Py_XDECREF(file);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
@ -3048,10 +3062,14 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
|
|||
return 0;
|
||||
}
|
||||
|
||||
PyObject *ferr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
|
||||
PyObject *ferr;
|
||||
if (_PySys_GetOptionalAttr(&_Py_ID(stderr), &ferr) < 0) {
|
||||
_PyErr_Clear(tstate);
|
||||
}
|
||||
if (ferr == NULL || ferr == Py_None) {
|
||||
/* sys.stderr is not set yet or set to None,
|
||||
no need to try to display the exception */
|
||||
Py_XDECREF(ferr);
|
||||
Py_DECREF(exc);
|
||||
return 0;
|
||||
}
|
||||
|
@ -3067,6 +3085,7 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
|
|||
if (_PyFile_Flush(ferr) < 0) {
|
||||
_PyErr_Clear(tstate);
|
||||
}
|
||||
Py_DECREF(ferr);
|
||||
|
||||
return has_tb;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue