aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/traceback.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2025-02-25 23:04:27 +0200
committerGitHub <noreply@github.com>2025-02-25 23:04:27 +0200
commit0ef4ffeefd1737c18dc9326133c7894d58108c2e (patch)
treedc3614077d06e68df192e57505951b80012a2e31 /Python/traceback.c
parent2dad1e08ec9d5ddc798a313900613b3d1eeaff6b (diff)
downloadcpython-0ef4ffeefd1737c18dc9326133c7894d58108c2e.tar.gz
cpython-0ef4ffeefd1737c18dc9326133c7894d58108c2e.zip
gh-130163: Fix crashes related to PySys_GetObject() (GH-130503)
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().
Diffstat (limited to 'Python/traceback.c')
-rw-r--r--Python/traceback.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/Python/traceback.c b/Python/traceback.c
index 870ae5bcefe..e53d87d52f9 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -12,7 +12,7 @@
#include "pycore_pyarena.h" // _PyArena_Free()
#include "pycore_pyerrors.h" // _PyErr_GetRaisedException()
#include "pycore_pystate.h" // _PyThreadState_GET()
-#include "pycore_sysmodule.h" // _PySys_GetAttr()
+#include "pycore_sysmodule.h" // _PySys_GetOptionalAttr()
#include "pycore_traceback.h" // EXCEPTION_TB_HEADER
#include "frameobject.h" // PyFrame_New()
@@ -356,9 +356,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));
@@ -401,6 +405,7 @@ error:
result = NULL;
finally:
Py_XDECREF(open);
+ Py_XDECREF(syspath);
Py_DECREF(filebytes);
return result;
}
@@ -729,17 +734,21 @@ _PyTraceBack_Print(PyObject *v, const char *header, PyObject *f)
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 (PyFile_WriteString(header, f) < 0) {
return -1;