diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index cc07062eee6f98..c903ee7a7f1cb5 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -4932,6 +4932,15 @@ def test_shared_memory_cleaned_after_process_termination(self): "resource_tracker: There appear to be 1 leaked " "shared_memory objects to clean up at shutdown", err) + def test_shared_memory_slice_assignment_no_crash(self): + from multiprocessing import shared_memory + shm = shared_memory.SharedMemory(create=True, size=10) + mv = shm.buf + shm.close() + shm.unlink() + with self.assertRaises((BufferError, ValueError)): + mv[:5] = b'hello' + @unittest.skipIf(os.name != "posix", "resource_tracker is posix only") @resource_tracker_format_subtests def test_shared_memory_untracking(self): diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-01-27-23-35-19.gh-issue-144281.IiakEV.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-01-27-23-35-19.gh-issue-144281.IiakEV.rst new file mode 100644 index 00000000000000..b9561e49509fb6 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-01-27-23-35-19.gh-issue-144281.IiakEV.rst @@ -0,0 +1,2 @@ +Fix a possible interpreter crash during memoryview slice assignment when the +underlying buffer is backed by shared memory. diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index f3b7e4a396b4a1..27819292425916 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -2656,6 +2656,11 @@ memory_ass_sub(PyObject *_self, PyObject *key, PyObject *value) CHECK_RELEASED_INT(self); + if (view->buf == NULL) { + PyErr_SetString(PyExc_BufferError, "memoryview: underlying buffer is no longer valid"); + return -1; + } + fmt = adjust_fmt(view); if (fmt == NULL) return -1;