Skip to content

Commit 40d7f74

Browse files
[3.12] pythongh-126108: Fix potential null pointer dereference in PySys_AddWarnOptionUnicode (pythonGH-126118) (python#129522)
pythongh-126108: Fix potential null pointer dereference in `PySys_AddWarnOptionUnicode` (pythonGH-126118) (cherry picked from commit fad36bf) Co-authored-by: Valery Fedorenko <[email protected]>
1 parent 48f08fe commit 40d7f74

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a possible ``NULL`` pointer dereference in :c:func:`!PySys_AddWarnOptionUnicode`.

Python/sysmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,6 +2653,7 @@ PySys_ResetWarnOptions(void)
26532653
static int
26542654
_PySys_AddWarnOptionWithError(PyThreadState *tstate, PyObject *option)
26552655
{
2656+
assert(tstate != NULL);
26562657
PyObject *warnoptions = get_warnoptions(tstate);
26572658
if (warnoptions == NULL) {
26582659
return -1;
@@ -2667,11 +2668,11 @@ void
26672668
PySys_AddWarnOptionUnicode(PyObject *option)
26682669
{
26692670
PyThreadState *tstate = _PyThreadState_GET();
2671+
_Py_EnsureTstateNotNULL(tstate);
2672+
assert(!_PyErr_Occurred(tstate));
26702673
if (_PySys_AddWarnOptionWithError(tstate, option) < 0) {
26712674
/* No return value, therefore clear error state if possible */
2672-
if (tstate) {
2673-
_PyErr_Clear(tstate);
2674-
}
2675+
_PyErr_Clear(tstate);
26752676
}
26762677
}
26772678

0 commit comments

Comments
 (0)