mirror of
https://github.com/python/cpython.git
synced 2026-01-07 07:52:29 +00:00
[3.12] gh-130163: Fix crashes related to PySys_GetObject() (GH-130503) (GH-130556) (GH-130576)
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 commit0ef4ffeefd) (cherry picked from commit7c1b76fce8) (cherry picked from commit2ab7e1135a)
This commit is contained in:
parent
6a268a046f
commit
89a79fc919
21 changed files with 506 additions and 162 deletions
|
|
@ -13,6 +13,7 @@
|
|||
#include "pycore_pyarena.h" // _PyArena_Free()
|
||||
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
|
||||
#include "pycore_pystate.h" // _PyThreadState_GET()
|
||||
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttr()
|
||||
#include "pycore_traceback.h" // EXCEPTION_TB_HEADER
|
||||
|
||||
#include "../Parser/pegen.h" // _PyPegen_byte_offset_to_character_offset()
|
||||
|
|
@ -349,9 +350,13 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *
|
|||
taillen = strlen(tail);
|
||||
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
syspath = _PySys_GetAttr(tstate, &_Py_ID(path));
|
||||
if (syspath == NULL || !PyList_Check(syspath))
|
||||
if (_PySys_GetOptionalAttr(&_Py_ID(path), &syspath) < 0) {
|
||||
PyErr_Clear();
|
||||
goto error;
|
||||
}
|
||||
if (syspath == NULL || !PyList_Check(syspath)) {
|
||||
goto error;
|
||||
}
|
||||
npath = PyList_Size(syspath);
|
||||
|
||||
open = PyObject_GetAttr(io, &_Py_ID(open));
|
||||
|
|
@ -394,6 +399,7 @@ _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *
|
|||
result = NULL;
|
||||
finally:
|
||||
Py_XDECREF(open);
|
||||
Py_XDECREF(syspath);
|
||||
Py_DECREF(filebytes);
|
||||
return result;
|
||||
}
|
||||
|
|
@ -1064,17 +1070,21 @@ _PyTraceBack_Print_Indented(PyObject *v, int indent, const char *margin,
|
|||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
}
|
||||
limitv = PySys_GetObject("tracebacklimit");
|
||||
if (limitv && PyLong_Check(limitv)) {
|
||||
if (_PySys_GetOptionalAttrString("tracebacklimit", &limitv) < 0) {
|
||||
return -1;
|
||||
}
|
||||
else if (limitv != NULL && PyLong_Check(limitv)) {
|
||||
int overflow;
|
||||
limit = PyLong_AsLongAndOverflow(limitv, &overflow);
|
||||
if (overflow > 0) {
|
||||
limit = LONG_MAX;
|
||||
}
|
||||
else if (limit <= 0) {
|
||||
Py_DECREF(limitv);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Py_XDECREF(limitv);
|
||||
if (_Py_WriteIndentedMargin(indent, header_margin, f) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue