From b3e8b3371d93451b0186cd28506e3c8b96186c37 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Thu, 3 Sep 2026 04:32:16 +0300 Subject: [PATCH] gh-156867: struct.pack() check for overflows for Zf type --- Lib/test/test_struct.py | 10 ++++++++++ .../2026-09-03-04-32-06.gh-issue-156867.hq3efC.rst | 3 +++ Modules/_struct.c | 10 +++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-03-04-32-06.gh-issue-156867.hq3efC.rst diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py index 3809f2d26438955..530bd8f65d74613 100644 --- a/Lib/test/test_struct.py +++ b/Lib/test/test_struct.py @@ -424,6 +424,16 @@ def test_705836(self): self.assertRaises(OverflowError, struct.pack, "Zf", big_real) + self.assertRaises(OverflowError, struct.pack, "Zf", big_imag) + self.assertRaises(OverflowError, struct.pack, "StructError, "required argument is not a complex"); return -1; } - memcpy(p, &x, sizeof(x)); - return 0; + + int ret = PyFloat_Pack4(c.real, p, PY_LITTLE_ENDIAN); + + if (ret) { + return ret; + } + return PyFloat_Pack4(c.imag, p + sizeof(float), PY_LITTLE_ENDIAN); } static int