bpo-31579: Fixed a possible leak in enumerate() with large indices. (… · python/cpython@0e950dd (original) (raw)

Original file line number Diff line number Diff line change
@@ -108,14 +108,18 @@ enum_next_long(enumobject *en, PyObject* next_item)
108 108
109 109 if (en->en_longindex == NULL) {
110 110 en->en_longindex = PyLong_FromSsize_t(PY_SSIZE_T_MAX);
111 -if (en->en_longindex == NULL)
111 +if (en->en_longindex == NULL) {
112 +Py_DECREF(next_item);
112 113 return NULL;
114 + }
113 115 }
114 116 next_index = en->en_longindex;
115 117 assert(next_index != NULL);
116 118 stepped_up = PyNumber_Add(next_index, _PyLong_One);
117 -if (stepped_up == NULL)
119 +if (stepped_up == NULL) {
120 +Py_DECREF(next_item);
118 121 return NULL;
122 + }
119 123 en->en_longindex = stepped_up;
120 124
121 125 if (result->ob_refcnt == 1) {