]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41344: Raise ValueError when creating shared memory of size 0 (GH-21556) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sun, 30 Aug 2020 19:42:27 +0000 (12:42 -0700)
committerGitHub <noreply@github.com>
Sun, 30 Aug 2020 19:42:27 +0000 (20:42 +0100)
(cherry picked from commit 475a5fbb5644ea200c990d85d8c264e78ab6c7ea)

Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>
Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>
Lib/multiprocessing/shared_memory.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst [new file with mode: 0644]

index f92eb012c83160e2e10a23e8c7c9741da3e53b9d..e5b5e6c098a021d1f2c36f4cfaba677107fc2008 100644 (file)
@@ -75,6 +75,8 @@ class SharedMemory:
             raise ValueError("'size' must be a positive integer")
         if create:
             self._flags = _O_CREX | os.O_RDWR
+            if size == 0:
+                raise ValueError("'size' must be a positive number different from zero")
         if name is None and not self._flags & os.O_EXCL:
             raise ValueError("'name' can only be None if create=True")
 
index 8626aa37c186e9e41b322204726420f9b3448fb6..ee9b47bb5f153d50d6f334d171aaab460c1b3b1c 100644 (file)
@@ -3827,6 +3827,18 @@ class _TestSharedMemory(BaseTestCase):
 
         sms.close()
 
+        # Test creating a shared memory segment with negative size
+        with self.assertRaises(ValueError):
+            sms_invalid = shared_memory.SharedMemory(create=True, size=-1)
+
+        # Test creating a shared memory segment with size 0
+        with self.assertRaises(ValueError):
+            sms_invalid = shared_memory.SharedMemory(create=True, size=0)
+
+        # Test creating a shared memory segment without size argument
+        with self.assertRaises(ValueError):
+            sms_invalid = shared_memory.SharedMemory(create=True)
+
     def test_shared_memory_across_processes(self):
         # bpo-40135: don't define shared memory block's name in case of
         # the failure when we run multiprocessing tests in parallel.
diff --git a/Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst b/Misc/NEWS.d/next/Library/2020-07-20-13-27-48.bpo-41344.iKipNd.rst
new file mode 100644 (file)
index 0000000..475bc9b
--- /dev/null
@@ -0,0 +1 @@
+Prevent creating :class:`shared_memory.SharedMemory` objects with :code:`size=0`.