]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-127563: use `dk_log2_index_bytes=3` in empty dicts (GH-127568) (GH-127813...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 16 Jun 2025 12:51:04 +0000 (14:51 +0200)
committerGitHub <noreply@github.com>
Mon, 16 Jun 2025 12:51:04 +0000 (14:51 +0200)
[3.12] gh-127563: use `dk_log2_index_bytes=3` in empty dicts (GH-127568) (GH-127813)

This fixes a UBSan failure (unaligned zero-size memcpy) in `dictobject.c`.
(cherry picked from commit 9af96f440618304e7cc609c246e1f8c8b2d7a119)

(cherry picked from commit 320a1dcd97110598a4fe6ae1c0018cd3decc408f)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Objects/dictobject.c

index 69a4a865103c182bc978353c83ea926dd988beb8..0e9c6d24e48338f5b3b3b4e4081f9f20cf772241 100644 (file)
@@ -449,11 +449,14 @@ estimate_log2_keysize(Py_ssize_t n)
 
 /* This immutable, empty PyDictKeysObject is used for PyDict_Clear()
  * (which cannot fail and thus can do no allocation).
+ *
+ * See https://github.com/python/cpython/pull/127568#discussion_r1868070614
+ * for the rationale of using dk_log2_index_bytes=3 instead of 0.
  */
 static PyDictKeysObject empty_keys_struct = {
         1, /* dk_refcnt */
         0, /* dk_log2_size */
-        0, /* dk_log2_index_bytes */
+        3, /* dk_log2_index_bytes */
         DICT_KEYS_UNICODE, /* dk_kind */
         1, /* dk_version */
         0, /* dk_usable (immutable) */