Skip to content

gh-156865: struct.pack() check for overflows for Zf type - #156868

Open
skirpichev wants to merge 5 commits into
python:mainfrom
skirpichev:overflows-in-Zf/156867
Open

skirpichev wants to merge 5 commits into
python:mainfrom
skirpichev:overflows-in-Zf/156867

Conversation

@skirpichev

@skirpichev skirpichev commented Sep 3, 2026

Copy link
Copy Markdown
Member

@skirpichev
skirpichev requested a review from vstinner September 3, 2026 01:38
@skirpichev skirpichev added needs backport to 3.14 bugs and security fixes 3.15 pre-release feature fixes, bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes and removed 3.15 pre-release feature fixes, bugs and security fixes labels Sep 3, 2026
@skirpichev skirpichev changed the title gh-156867: struct.pack() check for overflows for Zf type gh-156865: struct.pack() check for overflows for Zf type Sep 3, 2026
@skirpichev skirpichev removed needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 3, 2026
Comment thread Modules/_struct.c Outdated
Comment thread Lib/test/test_struct.py Outdated
@skirpichev
skirpichev requested a review from vstinner September 19, 2026 00:00
Comment thread Lib/test/test_struct.py
Comment on lines +436 to +441
ba = bytearray(8)
try:
struct.Struct(format).pack_into(ba, 0, value)
assert False
except OverflowError:
self.assertEqual(ba, bytearray(8))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using assertRaises(). "assert False" is removed if Python is run with -O option.

Suggested change
ba = bytearray(8)
try:
struct.Struct(format).pack_into(ba, 0, value)
assert False
except OverflowError:
self.assertEqual(ba, bytearray(8))
ba = bytearray(8)
with self.assertRaises(OverflowError):
struct.Struct(format).pack_into(ba, 0, value)
self.assertEqual(ba, bytearray(8))

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.

2 participants