Skip to content

Commit

Permalink
bpo-41344: Raise ValueError when creating shared memory of size 0 (GH…
Browse files Browse the repository at this point in the history
…-21556) (GH-22018)

(cherry picked from commit 475a5fb)

Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>

Co-authored-by: Vinay Sharma <vinay04sharma@icloud.com>
  • Loading branch information
miss-islington and vinay0410 committed Aug 30, 2020
1 parent 901c2ea commit ca55ecb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/multiprocessing/shared_memory.py
Expand Up @@ -76,6 +76,8 @@ def __init__(self, name=None, create=False, size=0):
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")

Expand Down
12 changes: 12 additions & 0 deletions Lib/test/_test_multiprocessing.py
Expand Up @@ -3864,6 +3864,18 @@ class OptionalAttachSharedMemory(shared_memory.SharedMemory):

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.
Expand Down
@@ -0,0 +1 @@
Prevent creating :class:`shared_memory.SharedMemory` objects with :code:`size=0`.

0 comments on commit ca55ecb

Please sign in to comment.