gh-152682: Fix NULL dereference on OOM in symtable_visit_type_param_bound_or_default (#152684)

In `symtable_visit_type_param_bound_or_default()`, when a reserved name
(e.g. `__classdict__`) is used as a type parameter, `PyUnicode_FromFormat()`
is called to build the SyntaxError message. If the allocation fails and
returns NULL, the subsequent `PyErr_SetObject()` and `Py_DECREF()` calls
would dereference NULL, causing a segfault.

Fix by returning 0 immediately when `PyUnicode_FromFormat()` returns NULL.
This propagates the MemoryError set by `PyUnicode_FromFormat()`.

The bug was introduced in gh-128632 (commit 891c61c).
This commit is contained in:
Petr Vaganov
2026-06-30 21:45:25 +07:00
committed by GitHub
parent 2303eea150
commit 10ed03edf1
3 changed files with 23 additions and 0 deletions
+3
View File
@@ -2678,6 +2678,9 @@ symtable_visit_type_param_bound_or_default(
PyObject *error_msg = PyUnicode_FromFormat("reserved name '%U' cannot be "
"used for type parameter", name);
if (error_msg == NULL) {
return 0;
}
PyErr_SetObject(PyExc_SyntaxError, error_msg);
Py_DECREF(error_msg);
SET_ERROR_LOCATION(st->st_filename, LOCATION(tp));