Skip to content

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

Merged
gpshead merged 1 commit into
python:3.14from
jelle-openai:jelle/codex/backport-159985-3.14
Nov 9, 2026
Merged

gpshead merged 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>
@gpshead
gpshead enabled auto-merge (squash) November 9, 2026 03:25
@gpshead
gpshead merged commit a96096c into python:3.14 Nov 9, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants