https://github.com/python/cpython/commit/0d5e2ed94fc130156957c359cd8176fd33312e75
commit: 0d5e2ed94fc130156957c359cd8176fd33312e75
branch: main
author: sobolevn <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-11-18T14:51:32+03:00
summary:

gh-161767: Fix crash in `PyDict_AsFrozenDictAndClear` on `embedded` values 
(#162139)

files:
M Doc/c-api/dict.rst
M Lib/test/test_capi/test_dict.py
M Objects/dictobject.c

diff --git a/Doc/c-api/dict.rst b/Doc/c-api/dict.rst
index e986c8b6456b749..cdb3d8fba5af80c 100644
--- a/Doc/c-api/dict.rst
+++ b/Doc/c-api/dict.rst
@@ -526,6 +526,8 @@ Dictionary objects
    as an argument, prefer :c:func:`PyFrozenDict_New` to leave the source
    intact.
 
+   Does not work for dicts with embedded values.
+
    .. impl-detail::
 
       Works with O(1) complexity.
diff --git a/Lib/test/test_capi/test_dict.py b/Lib/test/test_capi/test_dict.py
index 9a2e79d4da7e715..4028b56a9cc22c0 100644
--- a/Lib/test/test_capi/test_dict.py
+++ b/Lib/test/test_capi/test_dict.py
@@ -635,11 +635,47 @@ class DictSubtype(dict): ...
         self.assertIs(type(d), DictSubtype)
         self.assertEqual(d, DictSubtype({}))
 
+        # non str keys:
+        d = {1: 'a', 2: 'b', 3: 'c', None: 'f'}
+        f = as_frozendict(d)
+        self.assertIs(type(f), frozendict)
+        self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'}))
+        self.assertIs(type(d), dict)
+        self.assertEqual(d, {})
+
+        d = DictSubtype({1: 'a', 2: 'b', 3: 'c', None: 'f'})
+        f = as_frozendict(d)
+        self.assertIs(type(f), frozendict)
+        self.assertEqual(f, frozendict({1: 'a', 2: 'b', 3: 'c', None: 'f'}))
+        self.assertIs(type(d), DictSubtype)
+        self.assertEqual(d, DictSubtype({}))
+
+        # wrong inputs:
         for wrong_input in (frozendict(), [], None):
             with self.subTest(wrong_input=wrong_input):
                 with self.assertRaises(SystemError):
                     as_frozendict(wrong_input)
 
+        # gh-161767: check type with inline values and dict with embedded 
values
+        class Regular: ...
+
+        for klass in (Regular, _testcapi.ManagedDictType):
+            with self.subTest(klass=klass):
+                instance = klass()  # empty `.__dict__`
+                with self.assertRaisesRegex(ValueError, 'dict with embedded 
values'):
+                    as_frozendict(instance.__dict__)
+                self.assertEqual(instance.__dict__, {})
+
+                instance = klass()  # non-empty `.__dict__`
+                instance.key = 'value'
+                with self.assertRaisesRegex(ValueError, 'dict with embedded 
values'):
+                    as_frozendict(instance.__dict__)
+
+                self.assertEqual(instance.__dict__, {'key': 'value'})
+                # Both of these actions were needed to cause a crash:
+                instance.other = 'new'
+                del instance  # must not crash
+
     def test_frozendict_check(self):
         # Test PyFrozenDict_Check()
         check = _testcapi.frozendict_check
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 5d3edbc4ed9578a..ed8b53b8a08d495 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -8663,16 +8663,22 @@ frozendict_new(PyTypeObject *type, PyObject *args, 
PyObject *kwds)
     return d;
 }
 
-static void
+static int
 transfer_keys_and_values_lock_held(PyObject *res, PyObject *dict)
 {
     PyDictObject *new = (PyDictObject *)res;
     PyDictObject *old = (PyDictObject *)dict;
     assert(can_modify_dict(old));
 
+    if (old->ma_values && old->ma_values->embedded) {
+        PyErr_SetString(PyExc_ValueError,
+                        "can't convert dict with embedded values");
+        return -1;
+    }
+
     // Fast path: do nothing on an empty dict:
     if (old->ma_keys == Py_EMPTY_KEYS) {
-        return;
+        return 0;
     }
 
     Py_ssize_t used = old->ma_used;
@@ -8690,6 +8696,7 @@ transfer_keys_and_values_lock_held(PyObject *res, 
PyObject *dict)
     new->ma_keys = keys;
     new->ma_values = values;
     ASSERT_CONSISTENT(new);
+    return 0;
 }
 
 PyObject *
@@ -8705,9 +8712,13 @@ PyDict_AsFrozenDictAndClear(PyObject *dict)
         return NULL;
     }
 
+    int transfer;
     Py_BEGIN_CRITICAL_SECTION(dict);
-    transfer_keys_and_values_lock_held(res, dict);
+    transfer = transfer_keys_and_values_lock_held(res, dict);
     Py_END_CRITICAL_SECTION();
+    if (transfer < 0) {
+        return NULL;
+    }
 
     _PyObject_GC_TRACK(res);
     assert(_PyFrozenDictObject_CAST(res)->ma_hash == -1);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to