Skip to content

Commit 0d5e2ed

Browse files
authored
pythongh-161767: Fix crash in PyDict_AsFrozenDictAndClear on embedded values (python#162139)
1 parent b219e55 commit 0d5e2ed

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎Doc/c-api/dict.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ Dictionary objects
526526
as an argument, prefer :c:func:`PyFrozenDict_New` to leave the source
527527
intact.
528528
529+
Does not work for dicts with embedded values.
530+
529531
.. impl-detail::
530532
531533
Works with O(1) complexity.

‎Lib/test/test_capi/test_dict.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,47 @@ class DictSubtype(dict): ...
635635
self.assertIs(type(d), DictSubtype)
636636
self.assertEqual(d, DictSubtype({}))
637637

638+
# non str keys:
639+
d = {1: 'a', 2: 'b', 3: 'c', None: 'f'}
640+
f = as_frozendict(d)
641+
self.assertIs(type(f), frozendict)
642+
self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'}))
643+
self.assertIs(type(d), dict)
644+
self.assertEqual(d, {})
645+
646+
d = DictSubtype({1: 'a', 2: 'b', 3: 'c', None: 'f'})
647+
f = as_frozendict(d)
648+
self.assertIs(type(f), frozendict)
649+
self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'}))
650+
self.assertIs(type(d), DictSubtype)
651+
self.assertEqual(d, DictSubtype({}))
652+
653+
# wrong inputs:
638654
for wrong_input in (frozendict(), [], None):
639655
with self.subTest(wrong_input=wrong_input):
640656
with self.assertRaises(SystemError):
641657
as_frozendict(wrong_input)
642658

659+
# gh-161767: check type with inline values and dict with embedded values
660+
class Regular: ...
661+
662+
for klass in (Regular, _testcapi.ManagedDictType):
663+
with self.subTest(klass=klass):
664+
instance = klass() # empty `.__dict__`
665+
with self.assertRaisesRegex(ValueError, 'dict with embedded values'):
666+
as_frozendict(instance.__dict__)
667+
self.assertEqual(instance.__dict__, {})
668+
669+
instance = klass() # non-empty `.__dict__`
670+
instance.key = 'value'
671+
with self.assertRaisesRegex(ValueError, 'dict with embedded values'):
672+
as_frozendict(instance.__dict__)
673+
674+
self.assertEqual(instance.__dict__, {'key': 'value'})
675+
# Both of these actions were needed to cause a crash:
676+
instance.other = 'new'
677+
del instance # must not crash
678+
643679
def test_frozendict_check(self):
644680
# Test PyFrozenDict_Check()
645681
check = _testcapi.frozendict_check

‎Objects/dictobject.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8663,16 +8663,22 @@ frozendict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
86638663
return d;
86648664
}
86658665

8666-
static void
8666+
static int
86678667
transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict)
86688668
{
86698669
PyDictObject *new = (PyDictObject *)res;
86708670
PyDictObject *old = (PyDictObject *)dict;
86718671
assert(can_modify_dict(old));
86728672

8673+
if (old->ma_values && old->ma_values->embedded) {
8674+
PyErr_SetString(PyExc_ValueError,
8675+
"can't convert dict with embedded values");
8676+
return -1;
8677+
}
8678+
86738679
// Fast path: do nothing on an empty dict:
86748680
if (old->ma_keys == Py_EMPTY_KEYS) {
8675-
return;
8681+
return 0;
86768682
}
86778683

86788684
Py_ssize_t used = old->ma_used;
@@ -8690,6 +8696,7 @@ transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict)
86908696
new->ma_keys = keys;
86918697
new->ma_values = values;
86928698
ASSERT_CONSISTENT(new);
8699+
return 0;
86938700
}
86948701

86958702
PyObject *
@@ -8705,9 +8712,13 @@ PyDict_AsFrozenDictAndClear(PyObject *dict)
87058712
return NULL;
87068713
}
87078714

8715+
int transfer;
87088716
Py_BEGIN_CRITICAL_SECTION(dict);
8709-
transfer_keys_and_values_lock_held(res, dict);
8717+
transfer = transfer_keys_and_values_lock_held(res, dict);
87108718
Py_END_CRITICAL_SECTION();
8719+
if (transfer < 0) {
8720+
return NULL;
8721+
}
87118722

87128723
_PyObject_GC_TRACK(res);
87138724
assert(_PyFrozenDictObject_CAST(res)->ma_hash == -1);

0 commit comments

Comments
 (0)