Skip to content

Commit eca0efe

Browse files
miss-islingtonpetrvaganoffStanFromIreland
authored
[3.13] gh-152682: Fix NULL dereference on OOM in symtable_visit_type_param_bound_or_default (GH-152684) (#152697)
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 43d8492 commit eca0efe

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
@@ -2318,6 +2318,9 @@ symtable_visit_type_param_bound_or_default(
23182318

23192319
PyObject *error_msg = PyUnicode_FromFormat("reserved name '%U' cannot be "
23202320
"used for type parameter", name);
2321+
if (error_msg == NULL) {
2322+
return 0;
2323+
}
23212324
PyErr_SetObject(PyExc_SyntaxError, error_msg);
23222325
Py_DECREF(error_msg);
23232326
PyErr_RangedSyntaxLocationObject(st->st_filename,

0 commit comments

Comments
 (0)