From 8e6e81336ed277bba51969a08d7f31c1f6b8e6ea Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Wed, 2 Dec 2026 15:42:27 +0800 Subject: [PATCH 1/3] Fix ShareableList layout for empty str/bytes --- Lib/multiprocessing/shared_memory.py | 8 +++++++- Lib/test/_test_multiprocessing.py | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py index de74a0cbbba853..93184c0dbd8b3b 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -322,7 +322,13 @@ def __init__(self, sequence=None, *, name=None): self._types_mapping[type(item)] if not isinstance(item, (str, bytes)) else self._types_mapping[type(item)] % ( - self._alignment * ((len(self._encode_value(item)) - 1) // self._alignment + 1), + # An empty str/bytes still needs one alignment unit of + # storage: a zero-length slot would overlap the next + # item (or the metadata area), and __setitem__() could + # then silently overwrite it with a fixed-size value. + max(self._alignment, + self._alignment * ((len(self._encode_value(item)) - 1) + // self._alignment + 1)), ) for item in sequence ] diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 540189147cb4d7..19c6b2b3b97d2e 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5254,6 +5254,27 @@ def test_shared_memory_ShareableList_basics(self): finally: empty_sl.shm.unlink() + def test_shared_memory_ShareableList_empty_items(self): + # An empty str or bytes still needs a slot of its own: a zero-length + # slot overlaps the next item (or the metadata area), so storing a + # fixed-size value into it used to overwrite the following item, or + # to corrupt the packing formats and raise struct.error. + sl = shared_memory.ShareableList(['', b'', 42]) + self.addCleanup(sl.shm.unlink) + try: + self.assertEqual(sl.format, '8s8sq') + self.assertEqual(list(sl), ['', b'', 42]) + sl[0] = 7 + sl[1] = 8 + self.assertEqual(list(sl), [7, 8, 42]) + sl[2] = '' + self.assertEqual(list(sl), [7, 8, '']) + # A trailing empty item must not run into the metadata area. + sl[2] = 3 + self.assertEqual(list(sl), [7, 8, 3]) + finally: + sl.shm.close() + def test_shared_memory_ShareableList_unpackable_value(self): # Values of a supported type that cannot be packed must not leave # the newly created shared memory block behind. From 04d6eaf0d5b68d49636a195ac61cf2d8898bd04c Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 02:41:24 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst diff --git a/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst b/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst new file mode 100644 index 00000000000000..3a0fddbed765ff --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst @@ -0,0 +1,2 @@ +Fix a zero-length slot for an empty :class:`str` or :class:`bytes` in +:class:`multiprocessing.shared_memory.ShareableList`. From 176e90df43f5575772e143469fa7ff643df31751 Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 3 Dec 2026 11:23:41 +0800 Subject: [PATCH 3/3] Fix NEWS --- .../next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst b/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst index 3a0fddbed765ff..30d69549827bae 100644 --- a/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst +++ b/Misc/NEWS.d/next/Library/2026-12-03-02-41-21.gh-issue-163051.APE7KX.rst @@ -1,2 +1,2 @@ -Fix a zero-length slot for an empty :class:`str` or :class:`bytes` in +Fix a zero-length slot for an empty :class:`str` or :class:`bytes` in :class:`multiprocessing.shared_memory.ShareableList`.