diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2025-02-26 15:20:47 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-26 15:20:47 (GMT) |
| commit | 89a79fc919419bfe817da13bc2a4437908d7fc07 (patch) | |
| tree | d8ca3bac7229ea443ff9d591b0459caac0b67cb1 /Python/sysmodule.c | |
| parent | 6a268a046f474e860c8d0186a859fdd075d80463 (diff) | |
| download | cpython-89a79fc919419bfe817da13bc2a4437908d7fc07.zip cpython-89a79fc919419bfe817da13bc2a4437908d7fc07.tar.gz cpython-89a79fc919419bfe817da13bc2a4437908d7fc07.tar.bz2 | |
[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 commit 0ef4ffeefd1737c18dc9326133c7894d58108c2e)
(cherry picked from commit 7c1b76fce8c8df00da38830f72dbdde6881a33be)
(cherry picked from commit 2ab7e1135a2d5ca45b60881ece27729e4fc0ee8b)
Diffstat (limited to 'Python/sysmodule.c')
| -rw-r--r-- | Python/sysmodule.c | 177 |
1 files changed, 146 insertions, 31 deletions
diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 1f7cc65..e3bdff1 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -29,6 +29,7 @@ Data members: #include "pycore_pymem.h" // _PyMem_SetDefaultAllocator() #include "pycore_pystate.h" // _PyThreadState_GET() #include "pycore_structseq.h" // _PyStructSequence_InitBuiltinWithFlags() +#include "pycore_sysmodule.h" #include "pycore_tuple.h" // _PyTuple_FromArray() #include "frameobject.h" // PyFrame_FastToLocalsWithError() @@ -78,23 +79,97 @@ _PySys_GetAttr(PyThreadState *tstate, PyObject *name) return value; } -static PyObject * -_PySys_GetObject(PyInterpreterState *interp, const char *name) + PyObject * +_PySys_GetRequiredAttr(PyObject *name) { - PyObject *sysdict = interp->sysdict; + if (!PyUnicode_Check(name)) { + PyErr_Format(PyExc_TypeError, + "attribute name must be string, not '%.200s'", + Py_TYPE(name)->tp_name); + return NULL; + } + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; if (sysdict == NULL) { + PyErr_SetString(PyExc_RuntimeError, "no sys module"); return NULL; } - return _PyDict_GetItemStringWithError(sysdict, name); + PyObject *value = _PyDict_GetItemWithError(sysdict, name); + Py_XINCREF(value); + if (value == NULL && !_PyErr_Occurred(tstate)) { + PyErr_Format(PyExc_RuntimeError, "lost sys.%U", name); + } + return value; } PyObject * -PySys_GetObject(const char *name) +_PySys_GetRequiredAttrString(const char *name) { PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + PyErr_SetString(PyExc_RuntimeError, "no sys module"); + return NULL; + } + PyObject *value = _PyDict_GetItemStringWithError(sysdict, name); + Py_XINCREF(value); + if (value == NULL && !_PyErr_Occurred(tstate)) { + PyErr_Format(PyExc_RuntimeError, "lost sys.%s", name); + } + return value; +} +int +_PySys_GetOptionalAttr(PyObject *name, PyObject **value) +{ + if (!PyUnicode_Check(name)) { + PyErr_Format(PyExc_TypeError, + "attribute name must be string, not '%.200s'", + Py_TYPE(name)->tp_name); + *value = NULL; + return -1; + } + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + *value = NULL; + return 0; + } + *value = _PyDict_GetItemWithError(sysdict, name); + if (*value) { + Py_INCREF(*value); + return 1; + } + return _PyErr_Occurred(tstate) ? -1 : 0; +} + +int +_PySys_GetOptionalAttrString(const char *name, PyObject **value) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + *value = NULL; + return 0; + } + *value = _PyDict_GetItemStringWithError(sysdict, name); + if (*value) { + Py_INCREF(*value); + return 1; + } + return _PyErr_Occurred(tstate) ? -1 : 0; +} + +PyObject * +PySys_GetObject(const char *name) +{ + PyThreadState *tstate = _PyThreadState_GET(); + PyObject *sysdict = tstate->interp->sysdict; + if (sysdict == NULL) { + return NULL; + } PyObject *exc = _PyErr_GetRaisedException(tstate); - PyObject *value = _PySys_GetObject(tstate->interp, name); + PyObject *value = _PyDict_GetItemStringWithError(sysdict, name); /* XXX Suppress a new exception if it was raised and restore * the old one. */ _PyErr_SetRaisedException(tstate, exc); @@ -108,6 +183,10 @@ sys_set_object(PyInterpreterState *interp, PyObject *key, PyObject *v) return -1; } PyObject *sd = interp->sysdict; + if (sd == NULL) { + PyErr_SetString(PyExc_RuntimeError, "no sys module"); + return -1; + } if (v == NULL) { v = _PyDict_Pop(sd, key, Py_None); if (v == NULL) { @@ -724,9 +803,13 @@ sys_displayhook(PyObject *module, PyObject *o) } if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), Py_None) != 0) return NULL; - outf = _PySys_GetAttr(tstate, &_Py_ID(stdout)); - if (outf == NULL || outf == Py_None) { + outf = _PySys_GetRequiredAttr(&_Py_ID(stdout)); + if (outf == NULL) { + return NULL; + } + if (outf == Py_None) { _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.stdout"); + Py_DECREF(outf); return NULL; } if (PyFile_WriteObject(o, outf, 0) != 0) { @@ -737,17 +820,23 @@ sys_displayhook(PyObject *module, PyObject *o) _PyErr_Clear(tstate); err = sys_displayhook_unencodable(outf, o); if (err) { + Py_DECREF(outf); return NULL; } } else { + Py_DECREF(outf); return NULL; } } - if (PyFile_WriteObject(_Py_LATIN1_CHR('\n'), outf, Py_PRINT_RAW) != 0) + if (PyFile_WriteObject(_Py_LATIN1_CHR('\n'), outf, Py_PRINT_RAW) != 0) { + Py_DECREF(outf); return NULL; - if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), o) != 0) + } + Py_DECREF(outf); + if (PyObject_SetAttr(builtins, _Py_LATIN1_CHR('_'), o) != 0) { return NULL; + } Py_RETURN_NONE; } @@ -2610,7 +2699,10 @@ _PySys_ReadPreinitXOptions(PyConfig *config) static PyObject * get_warnoptions(PyThreadState *tstate) { - PyObject *warnoptions = _PySys_GetAttr(tstate, &_Py_ID(warnoptions)); + PyObject *warnoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) { + return NULL; + } if (warnoptions == NULL || !PyList_Check(warnoptions)) { /* PEP432 TODO: we can reach this if warnoptions is NULL in the main * interpreter config. When that happens, we need to properly set @@ -2622,6 +2714,7 @@ get_warnoptions(PyThreadState *tstate) * call optional for embedding applications, thus making this * reachable again. */ + Py_XDECREF(warnoptions); warnoptions = PyList_New(0); if (warnoptions == NULL) { return NULL; @@ -2630,7 +2723,6 @@ get_warnoptions(PyThreadState *tstate) Py_DECREF(warnoptions); return NULL; } - Py_DECREF(warnoptions); } return warnoptions; } @@ -2644,10 +2736,15 @@ PySys_ResetWarnOptions(void) return; } - PyObject *warnoptions = _PySys_GetAttr(tstate, &_Py_ID(warnoptions)); - if (warnoptions == NULL || !PyList_Check(warnoptions)) + PyObject *warnoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) { + PyErr_Clear(); return; - PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); + } + if (warnoptions != NULL && PyList_Check(warnoptions)) { + PyList_SetSlice(warnoptions, 0, PyList_GET_SIZE(warnoptions), NULL); + } + Py_XDECREF(warnoptions); } static int @@ -2659,8 +2756,10 @@ _PySys_AddWarnOptionWithError(PyThreadState *tstate, PyObject *option) return -1; } if (PyList_Append(warnoptions, option)) { + Py_DECREF(warnoptions); return -1; } + Py_DECREF(warnoptions); return 0; } @@ -2698,16 +2797,24 @@ _Py_COMP_DIAG_POP int PySys_HasWarnOptions(void) { - PyThreadState *tstate = _PyThreadState_GET(); - PyObject *warnoptions = _PySys_GetAttr(tstate, &_Py_ID(warnoptions)); - return (warnoptions != NULL && PyList_Check(warnoptions) - && PyList_GET_SIZE(warnoptions) > 0); + PyObject *warnoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(warnoptions), &warnoptions) < 0) { + PyErr_Clear(); + return 0; + } + int r = (warnoptions != NULL && PyList_Check(warnoptions) && + PyList_GET_SIZE(warnoptions) > 0); + Py_XDECREF(warnoptions); + return r; } static PyObject * get_xoptions(PyThreadState *tstate) { - PyObject *xoptions = _PySys_GetAttr(tstate, &_Py_ID(_xoptions)); + PyObject *xoptions; + if (_PySys_GetOptionalAttr(&_Py_ID(_xoptions), &xoptions) < 0) { + return NULL; + } if (xoptions == NULL || !PyDict_Check(xoptions)) { /* PEP432 TODO: we can reach this if xoptions is NULL in the main * interpreter config. When that happens, we need to properly set @@ -2719,6 +2826,7 @@ get_xoptions(PyThreadState *tstate) * call optional for embedding applications, thus making this * reachable again. */ + Py_XDECREF(xoptions); xoptions = PyDict_New(); if (xoptions == NULL) { return NULL; @@ -2727,7 +2835,6 @@ get_xoptions(PyThreadState *tstate) Py_DECREF(xoptions); return NULL; } - Py_DECREF(xoptions); } return xoptions; } @@ -2766,11 +2873,13 @@ _PySys_AddXOptionWithError(const wchar_t *s) } Py_DECREF(name); Py_DECREF(value); + Py_DECREF(opts); return 0; error: Py_XDECREF(name); Py_XDECREF(value); + Py_XDECREF(opts); return -1; } @@ -2792,7 +2901,9 @@ PyObject * PySys_GetXOptions(void) { PyThreadState *tstate = _PyThreadState_GET(); - return get_xoptions(tstate); + PyObject *opts = get_xoptions(tstate); + Py_XDECREF(opts); + return opts; } /* XXX This doc string is too long to be a single string literal in VC++ 5.0. @@ -3517,16 +3628,15 @@ _PySys_UpdateConfig(PyThreadState *tstate) #undef COPY_WSTR // sys.flags - PyObject *flags = _PySys_GetObject(interp, "flags"); // borrowed ref + PyObject *flags = _PySys_GetRequiredAttrString("flags"); if (flags == NULL) { - if (!_PyErr_Occurred(tstate)) { - _PyErr_SetString(tstate, PyExc_RuntimeError, "lost sys.flags"); - } return -1; } if (set_flags_from_config(interp, flags) < 0) { + Py_DECREF(flags); return -1; } + Py_DECREF(flags); SET_SYS("dont_write_bytecode", PyBool_FromLong(!config->write_bytecode)); @@ -3754,12 +3864,15 @@ PySys_SetArgvEx(int argc, wchar_t **argv, int updatepath) Py_FatalError("can't compute path0 from argv"); } - PyObject *sys_path = _PySys_GetAttr(tstate, &_Py_ID(path)); - if (sys_path != NULL) { + PyObject *sys_path; + if (_PySys_GetOptionalAttr(&_Py_ID(path), &sys_path) < 0) { + Py_FatalError("can't get sys.path"); + } + else if (sys_path != NULL) { if (PyList_Insert(sys_path, 0, path0) < 0) { - Py_DECREF(path0); Py_FatalError("can't prepend path0 to sys.path"); } + Py_DECREF(sys_path); } Py_DECREF(path0); } @@ -3847,8 +3960,8 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va) PyThreadState *tstate = _PyThreadState_GET(); PyObject *exc = _PyErr_GetRaisedException(tstate); - file = _PySys_GetAttr(tstate, key); written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va); + file = _PySys_GetRequiredAttr(key); if (sys_pyfile_write(buffer, file) != 0) { _PyErr_Clear(tstate); fputs(buffer, fp); @@ -3858,6 +3971,7 @@ sys_write(PyObject *key, FILE *fp, const char *format, va_list va) if (sys_pyfile_write(truncated, file) != 0) fputs(truncated, fp); } + Py_XDECREF(file); _PyErr_SetRaisedException(tstate, exc); } @@ -3889,15 +4003,16 @@ sys_format(PyObject *key, FILE *fp, const char *format, va_list va) PyThreadState *tstate = _PyThreadState_GET(); PyObject *exc = _PyErr_GetRaisedException(tstate); - file = _PySys_GetAttr(tstate, key); message = PyUnicode_FromFormatV(format, va); if (message != NULL) { + file = _PySys_GetRequiredAttr(key); if (sys_pyfile_write_unicode(message, file) != 0) { _PyErr_Clear(tstate); utf8 = PyUnicode_AsUTF8(message); if (utf8 != NULL) fputs(utf8, fp); } + Py_XDECREF(file); Py_DECREF(message); } _PyErr_SetRaisedException(tstate, exc); |
