bpo-36473: add maximum iteration check for dict .values() and .items() (GH-12619)
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 7ea979c..bba27dd 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -3630,6 +3630,12 @@
goto fail;
value = entry_ptr->me_value;
}
+ // We found an element, but did not expect it
+ if (di->len == 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "dictionary keys changed during iteration");
+ goto fail;
+ }
di->di_pos = i+1;
di->len--;
Py_INCREF(value);
@@ -3713,6 +3719,12 @@
key = entry_ptr->me_key;
value = entry_ptr->me_value;
}
+ // We found an element, but did not expect it
+ if (di->len == 0) {
+ PyErr_SetString(PyExc_RuntimeError,
+ "dictionary keys changed during iteration");
+ goto fail;
+ }
di->di_pos = i+1;
di->len--;
Py_INCREF(key);