gh-156939: Fix struct.pack('0p', bytes) - #157071
Conversation
If the Pascal string is empty (size=0), do not write the size prefix. Previously, a NUL byte was written outsize the buffer (buffer overflow).
|
In Python 3.14 and older, On Python 3.15 and newer, struct.pack() uses Well, it's better to avoid a buffer overflow anyway :-) |
|
Ah, I forgot to mention that the |
Oh, I forgot that Python 3.13 and 3.14 uses the old internal |
|
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
|
GH-157129 is a backport of this pull request to the 3.15 branch. |
|
GH-157130 is a backport of this pull request to the 3.14 branch. |
|
GH-157131 is a backport of this pull request to the 3.13 branch. |
gh-156939: Fix struct.pack('0p', bytes) (GH-157071) If the Pascal string is empty (size=0), do not write the size prefix. Previously, a NUL byte was written outsize the buffer (buffer overflow). In practice, the write remains into allocated memory and is silently ignored: no memory is corrupted. (cherry picked from commit 23525c9) Co-authored-by: Victor Stinner <vstinner@python.org>
gh-156939: Fix struct.pack('0p', bytes) (GH-157071) If the Pascal string is empty (size=0), do not write the size prefix. Previously, a NUL byte was written outsize the buffer (buffer overflow). In practice, the write remains into allocated memory and is silently ignored: no memory is corrupted. (cherry picked from commit 23525c9) Co-authored-by: Victor Stinner <vstinner@python.org>
|
If the import struct
size = 512
res = struct.pack(f'{size}s0p', b'x' * size, b'ignored')
print(len(res)) |
If the Pascal string is empty (size=0), do not write the size prefix. Previously, a NUL byte was written outsize the buffer (buffer overflow).