mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
Merge 7712af20f3 into f9704f1d84
This commit is contained in:
commit
1bc96b9b5d
3 changed files with 23 additions and 0 deletions
|
|
@ -2135,6 +2135,22 @@ class TestCounter(unittest.TestCase):
|
|||
self.assertEqual(c.setdefault('e', 5), 5)
|
||||
self.assertEqual(c['e'], 5)
|
||||
|
||||
def test_update_reentrant_add_clears_counter(self):
|
||||
c = Counter()
|
||||
key = object()
|
||||
|
||||
class Evil(int):
|
||||
def __new__(cls):
|
||||
return int.__new__(cls, 0)
|
||||
|
||||
def __add__(self, other):
|
||||
c.clear()
|
||||
return NotImplemented
|
||||
|
||||
c[key] = Evil()
|
||||
c.update([key])
|
||||
self.assertEqual(c[key], 1)
|
||||
|
||||
def test_init(self):
|
||||
self.assertEqual(list(Counter(self=42).items()), [('self', 42)])
|
||||
self.assertEqual(list(Counter(iterable=42).items()), [('iterable', 42)])
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Fix a potential use-after-free in :meth:`collections.Counter.update` when user code
|
||||
mutates the Counter during an update. (gh-143004)
|
||||
|
|
@ -2577,7 +2577,12 @@ _collections__count_elements_impl(PyObject *module, PyObject *mapping,
|
|||
if (_PyDict_SetItem_KnownHash(mapping, key, one, hash) < 0)
|
||||
goto done;
|
||||
} else {
|
||||
/* oldval is a borrowed reference. Keep it alive across
|
||||
PyNumber_Add(), which can execute arbitrary user code and
|
||||
mutate (or even clear) the underlying dict. */
|
||||
Py_INCREF(oldval);
|
||||
newval = PyNumber_Add(oldval, one);
|
||||
Py_DECREF(oldval);
|
||||
if (newval == NULL)
|
||||
goto done;
|
||||
if (_PyDict_SetItem_KnownHash(mapping, key, newval, hash) < 0)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue