diff options
author | Antoine Pitrou <antoine@python.org> | 2020-04-20 18:54:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-20 18:54:55 (GMT) |
commit | eba9f6155df59c9beed97fb5764c9f01dd941af0 (patch) | |
tree | f07cef322962795cd4ed0d71f74f9069580b16a5 /Lib/multiprocessing | |
parent | 5dd21f5d1c9b5a9316deca4535932675f04efeee (diff) | |
download | cpython-eba9f6155df59c9beed97fb5764c9f01dd941af0.zip cpython-eba9f6155df59c9beed97fb5764c9f01dd941af0.tar.gz cpython-eba9f6155df59c9beed97fb5764c9f01dd941af0.tar.bz2 |
bpo-40330: Fix utf-8 size check in ShareableList (GH-19606)
The item size must be checked after encoding to bytes, not before.
Automerge-Triggered-By: @pitrou
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r-- | Lib/multiprocessing/shared_memory.py | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/Lib/multiprocessing/shared_memory.py b/Lib/multiprocessing/shared_memory.py index 87e46cf..a3a5fcf 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -445,11 +445,14 @@ class ShareableList: if not isinstance(value, (str, bytes)): new_format = self._types_mapping[type(value)] + encoded_value = value else: allocated_length = self._allocated_offsets[position + 1] - item_offset - if len(value) > allocated_length: - raise ValueError("exceeds available storage for existing str") + encoded_value = (value.encode(_encoding) + if isinstance(value, str) else value) + if len(encoded_value) > allocated_length: + raise ValueError("bytes/str item exceeds available storage") if current_format[-1] == "s": new_format = current_format else: @@ -462,8 +465,7 @@ class ShareableList: new_format, value ) - value = value.encode(_encoding) if isinstance(value, str) else value - struct.pack_into(new_format, self.shm.buf, offset, value) + struct.pack_into(new_format, self.shm.buf, offset, encoded_value) def __reduce__(self): return partial(self.__class__, name=self.shm.name), () |