Skip to content

Commit ffb5184

Browse files
miss-islingtonpetrvaganoffStanFromIreland
authored
[3.15] gh-152682: Fix NULL dereference on OOM in symtable_visit_type_param_bound_or_default (GH-152684) (#152695)
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). (cherry picked from commit 10ed03e) Co-authored-by: Petr Vaganov <petrvaganoff@gmail.com> * Remove test --------- Co-authored-by: Petr Vaganov <petrvaganoff@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent fdba15a commit ffb5184

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix NULL pointer dereference in :func:`compile` when a reserved name (e.g.
2+
``__classdict__``) is used as a type parameter name and memory allocation
3+
fails while formatting the error message.

Python/symtable.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,6 +2678,9 @@ symtable_visit_type_param_bound_or_default(
26782678

26792679
PyObject *error_msg = PyUnicode_FromFormat("reserved name '%U' cannot be "
26802680
"used for type parameter", name);
2681+
if (error_msg == NULL) {
2682+
return 0;
2683+
}
26812684
PyErr_SetObject(PyExc_SyntaxError, error_msg);
26822685
Py_DECREF(error_msg);
26832686
SET_ERROR_LOCATION(st->st_filename, LOCATION(tp));

0 commit comments

Comments
 (0)