Skip to content

[3.14] gh-159739: Handle overlapping buffers in struct.pack_into (GH-159985) - #160484

Open
jelle-openai wants to merge 1 commit into
python:3.14from
jelle-openai:jelle/codex/backport-159985-3.14
Open

jelle-openai wants to merge 1 commit into
python:3.14from
jelle-openai:jelle/codex/backport-159985-3.14

Conversation

@jelle-openai

@jelle-openai jelle-openai commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

struct.pack_into() can use the same bytearray as both the destination and an s or p value. The copy then passes overlapping regions to memcpy, which is undefined in C and can produce platform-dependent results or a sanitizer abort. memmove makes the overlapping copy defined.
(cherry picked from commit 31a7d99)

Co-authored-by: Jelle Zijlstra jelle@openai.com

Backport of #159985 to 3.14.

Use overlap-safe copies in the 3.14 implementation of the s and p formats.

Ran test_struct. With an ASan-instrumented _struct, overlapping 8s and 8p copies both triggered memcpy-param-overlap on the original 3.14 code and completed with this change.

pythonGH-159985)

`struct.pack_into()` can use the same bytearray as both the destination and an `s` or `p` value. The copy then passes overlapping regions to `memcpy`, which is undefined in C and can produce platform-dependent results or a sanitizer abort. `memmove` makes the overlapping copy defined.
(cherry picked from commit 31a7d99)

Co-authored-by: Jelle Zijlstra <jelle@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant