diff options
author | Antoine Pitrou <antoine@python.org> | 2020-04-20 19:22:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-20 19:22:50 (GMT) |
commit | 887ff8e37e238fbce18c647e588283904f38ab24 (patch) | |
tree | 51e36929d3e7dcf3b89c54054028faf664bc4cf9 /Lib/multiprocessing/shared_memory.py | |
parent | 81de3c225774179cdc82a1733a64e4a876ff02b5 (diff) | |
download | cpython-887ff8e37e238fbce18c647e588283904f38ab24.zip cpython-887ff8e37e238fbce18c647e588283904f38ab24.tar.gz cpython-887ff8e37e238fbce18c647e588283904f38ab24.tar.bz2 |
[3.8] bpo-40330: Fix utf-8 size check in ShareableList (GH-19606) (GH-19625)
The item size must be checked after encoding to bytes, not before.
Automerge-Triggered-By: @pitrou.
(cherry picked from commit eba9f6155df59c9beed97fb5764c9f01dd941af0)
Co-authored-by: Antoine Pitrou <antoine@python.org>
Diffstat (limited to 'Lib/multiprocessing/shared_memory.py')
-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 184e367..f92eb01 100644 --- a/Lib/multiprocessing/shared_memory.py +++ b/Lib/multiprocessing/shared_memory.py @@ -433,9 +433,12 @@ class ShareableList: if not isinstance(value, (str, bytes)): new_format = self._types_mapping[type(value)] + encoded_value = value else: - if len(value) > self._allocated_bytes[position]: - raise ValueError("exceeds available storage for existing str") + encoded_value = (value.encode(_encoding) + if isinstance(value, str) else value) + if len(encoded_value) > self._allocated_bytes[position]: + raise ValueError("bytes/str item exceeds available storage") if current_format[-1] == "s": new_format = current_format else: @@ -448,8 +451,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), () |