[3.13] gh-127563: use dk_log2_index_bytes=3 in empty dicts (GH-127568) (GH-127798)

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

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-12-11 09:24:14 +01:00 committed by GitHub
parent 8b28dedc58
commit acf05aa5d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -624,11 +624,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 = {
_Py_IMMORTAL_REFCNT, /* dk_refcnt */
0, /* dk_log2_size */
0, /* dk_log2_index_bytes */
3, /* dk_log2_index_bytes */
DICT_KEYS_UNICODE, /* dk_kind */
#ifdef Py_GIL_DISABLED
{0}, /* dk_mutex */