Issue #24726: Revert setting the value on the dict if
_odict_add_new_node() fails.
diff --git a/Objects/odictobject.c b/Objects/odictobject.c
index 7a3ded0..98cbf94 100644
--- a/Objects/odictobject.c
+++ b/Objects/odictobject.c
@@ -1721,7 +1721,13 @@
int res = PyDict_SetItem(od, key, value);
if (res == 0) {
res = _odict_add_new_node((PyODictObject *)od, key);
- /* XXX Revert setting the value on the dict? */
+ if (res < 0) {
+ /* Revert setting the value on the dict */
+ PyObject *exc, *val, *tb;
+ PyErr_Fetch(&exc, &val, &tb);
+ (void) PyDict_DelItem(od, key);
+ _PyErr_ChainExceptions(exc, val, tb);
+ }
}
return res;
};