From 08dd04ef5ed4e819ca3e7db3dfa7c050c2ee66f6 Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 16:52:09 +0800 Subject: [PATCH 1/7] Fix incorrect error message for ctypes bitfield overflow. The previous message did not match the actual overflow check and omitted the bitfield size, making it misleading. Update the message to reflect the correct condition and report all relevant values. Signed-off-by: Yongtao Huang --- Modules/_ctypes/cfield.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index 547e2471a1cbc0..4ebca0e0b3db0a 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -160,8 +160,8 @@ PyCField_new_impl(PyTypeObject *type, PyObject *name, PyObject *proto, if ((bitfield_size + bit_offset) > byte_size * 8) { PyErr_Format( PyExc_ValueError, - "bit field %R overflows its type (%zd + %zd >= %zd)", - name, bit_offset, byte_size*8); + "bit field %R overflows its type (%zd + %zd > %zd)", + name, bit_offset, bitfield_size, byte_size * 8); goto error; } } From 862451baee9789ab1d25a3102983fc29dabadf8a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 25 Dec 2025 08:58:56 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst diff --git a/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst b/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst new file mode 100644 index 00000000000000..4c468c3beb75e9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst @@ -0,0 +1 @@ +Fix an incorrect error message for ctypes bitfield overflow. From 3042d0335bef9503f48f8e0f24e668b97e97c882 Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 19:04:23 +0800 Subject: [PATCH 3/7] Add test case --- Lib/test/test_ctypes/test_struct_fields.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index b50bbcbb65c423..2ea6b07d219b93 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -130,6 +130,21 @@ class S(Structure): self.check_struct(S) self.assertEqual(S.largeField.bit_size, size * 8) + def test_bitfield_overflow_error_message_gh143164(self): + with self.assertRaisesRegex( + ValueError, + r"bit field 'x' overflows its type \(0 \+ 9 > 8\)", + ): + CField( + name="x", + type=c_byte, + byte_size=1, + byte_offset=0, + index=0, + _internal_use=True, + bit_size=9, + bit_offset=0, + ) # __set__ and __get__ should raise a TypeError in case their self # argument is not a ctype instance. From 96f72e80204cd59375641e43845a5dff6a4fb35a Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 19:58:52 +0800 Subject: [PATCH 4/7] Update Lib/test/test_ctypes/test_struct_fields.py Co-authored-by: AN Long --- Lib/test/test_ctypes/test_struct_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 2ea6b07d219b93..69029f636c6f63 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -130,7 +130,7 @@ class S(Structure): self.check_struct(S) self.assertEqual(S.largeField.bit_size, size * 8) - def test_bitfield_overflow_error_message_gh143164(self): + def test_bitfield_overflow_error_message(self): with self.assertRaisesRegex( ValueError, r"bit field 'x' overflows its type \(0 \+ 9 > 8\)", From 739ab8f6b7dbb59ba52cd5f8b07fb6692836cd7e Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 21:48:56 +0800 Subject: [PATCH 5/7] Resolve comments --- Lib/test/test_ctypes/test_struct_fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index 69029f636c6f63..d6a313cbbde0e8 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -142,8 +142,8 @@ def test_bitfield_overflow_error_message(self): byte_offset=0, index=0, _internal_use=True, - bit_size=9, - bit_offset=0, + bit_size=7, + bit_offset=2, ) # __set__ and __get__ should raise a TypeError in case their self From 94d5656fff184508ffbff678e6b8ac353f18ebea Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 22:04:10 +0800 Subject: [PATCH 6/7] Update News.d --- .../next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst b/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst index 4c468c3beb75e9..e75270b9e94c03 100644 --- a/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst +++ b/Misc/NEWS.d/next/Library/2025-12-25-08-58-55.gh-issue-142164.XrFztf.rst @@ -1 +1 @@ -Fix an incorrect error message for ctypes bitfield overflow. +Fix the ctypes bitfield overflow error message to report the correct offset and size calculation. From 4a80197f2dee0b674a4535fc0c5688c2392941fd Mon Sep 17 00:00:00 2001 From: Yongtao Huang Date: Thu, 25 Dec 2025 22:12:37 +0800 Subject: [PATCH 7/7] Update test case error msg --- Lib/test/test_ctypes/test_struct_fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_ctypes/test_struct_fields.py b/Lib/test/test_ctypes/test_struct_fields.py index d6a313cbbde0e8..dc26e26d8a9fb1 100644 --- a/Lib/test/test_ctypes/test_struct_fields.py +++ b/Lib/test/test_ctypes/test_struct_fields.py @@ -133,7 +133,7 @@ class S(Structure): def test_bitfield_overflow_error_message(self): with self.assertRaisesRegex( ValueError, - r"bit field 'x' overflows its type \(0 \+ 9 > 8\)", + r"bit field 'x' overflows its type \(2 \+ 7 > 8\)", ): CField( name="x",