summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-09-08 19:28:18 (GMT)
committerGitHub <noreply@github.com>2023-09-08 19:28:18 (GMT)
commit50e4143f8dbbb6fb054a817df3331aea18773116 (patch)
treef3c72673d040d3f0babf6420a8f37449e36dc566 /Python
parent6b2f44ea7860bc9957e1f48ecddedff7a973876d (diff)
downloadcpython-50e4143f8dbbb6fb054a817df3331aea18773116.zip
cpython-50e4143f8dbbb6fb054a817df3331aea18773116.tar.gz
cpython-50e4143f8dbbb6fb054a817df3331aea18773116.tar.bz2
[3.11] Check the result of PySet_Contains() for error in Python/symtable.c (GH-109146) (GH-109158)
(cherry picked from commit 87a7faf6b68c8076e640a9a1347a255f132d8382)
Diffstat (limited to 'Python')
-rw-r--r--Python/symtable.c52
1 files changed, 40 insertions, 12 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index 0b259b0..afe05c7 100644
--- a/Python/symtable.c
+++ b/Python/symtable.c
@@ -502,6 +502,7 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
PyObject *bound, PyObject *local, PyObject *free,
PyObject *global)
{
+ int contains;
if (flags & DEF_GLOBAL) {
if (flags & DEF_NONLOCAL) {
PyErr_Format(PyExc_SyntaxError,
@@ -522,7 +523,11 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
"nonlocal declaration not allowed at module level");
return error_at_directive(ste, name);
}
- if (!PySet_Contains(bound, name)) {
+ contains = PySet_Contains(bound, name);
+ if (contains < 0) {
+ return 0;
+ }
+ if (!contains) {
PyErr_Format(PyExc_SyntaxError,
"no binding for nonlocal '%U' found",
name);
@@ -546,17 +551,29 @@ analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Note that having a non-NULL bound implies that the block
is nested.
*/
- if (bound && PySet_Contains(bound, name)) {
- SET_SCOPE(scopes, name, FREE);
- ste->ste_free = 1;
- return PySet_Add(free, name) >= 0;
+ if (bound) {
+ contains = PySet_Contains(bound, name);
+ if (contains < 0) {
+ return 0;
+ }
+ if (contains) {
+ SET_SCOPE(scopes, name, FREE);
+ ste->ste_free = 1;
+ return PySet_Add(free, name) >= 0;
+ }
}
/* If a parent has a global statement, then call it global
explicit? It could also be global implicit.
*/
- if (global && PySet_Contains(global, name)) {
- SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
- return 1;
+ if (global) {
+ contains = PySet_Contains(global, name);
+ if (contains < 0) {
+ return 0;
+ }
+ if (contains) {
+ SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
+ return 1;
+ }
}
if (ste->ste_nested)
ste->ste_free = 1;
@@ -590,8 +607,13 @@ analyze_cells(PyObject *scopes, PyObject *free)
scope = PyLong_AS_LONG(v);
if (scope != LOCAL)
continue;
- if (!PySet_Contains(free, name))
+ int contains = PySet_Contains(free, name);
+ if (contains < 0) {
+ goto error;
+ }
+ if (!contains) {
continue;
+ }
/* Replace LOCAL with CELL for this name, and remove
from free. It is safe to replace the value of name
in the dict, because it will not cause a resize.
@@ -691,9 +713,15 @@ update_symbols(PyObject *symbols, PyObject *scopes,
goto error;
}
/* Handle global symbol */
- if (bound && !PySet_Contains(bound, name)) {
- Py_DECREF(name);
- continue; /* it's a global */
+ if (bound) {
+ int contains = PySet_Contains(bound, name);
+ if (contains < 0) {
+ goto error;
+ }
+ if (!contains) {
+ Py_DECREF(name);
+ continue; /* it's a global */
+ }
}
/* Propagate new free symbol up the lexical stack */
if (PyDict_SetItem(symbols, name, v_free) < 0) {