summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_struct.py3
-rw-r--r--Misc/NEWS.d/next/Library/2024-10-09-07-09-00.gh-issue-125118.J9rQ1S.rst1
-rw-r--r--Modules/_struct.c5
3 files changed, 6 insertions, 3 deletions
diff --git a/Lib/test/test_struct.py b/Lib/test/test_struct.py
index e3193c7..04ec3ed 100644
--- a/Lib/test/test_struct.py
+++ b/Lib/test/test_struct.py
@@ -540,6 +540,9 @@ class StructTest(ComplexesAreIdenticalMixin, unittest.TestCase):
for c in [b'\x01', b'\x7f', b'\xff', b'\x0f', b'\xf0']:
self.assertTrue(struct.unpack('>?', c)[0])
+ self.assertTrue(struct.unpack('<?', c)[0])
+ self.assertTrue(struct.unpack('=?', c)[0])
+ self.assertTrue(struct.unpack('@?', c)[0])
def test_count_overflow(self):
hugecount = '{}b'.format(sys.maxsize+1)
diff --git a/Misc/NEWS.d/next/Library/2024-10-09-07-09-00.gh-issue-125118.J9rQ1S.rst b/Misc/NEWS.d/next/Library/2024-10-09-07-09-00.gh-issue-125118.J9rQ1S.rst
new file mode 100644
index 0000000..5d57cdb
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-09-07-09-00.gh-issue-125118.J9rQ1S.rst
@@ -0,0 +1 @@
+Don't copy arbitrary values to :c:expr:`_Bool` in the :mod:`struct` module.
diff --git a/Modules/_struct.c b/Modules/_struct.c
index 4387c55..21582b9 100644
--- a/Modules/_struct.c
+++ b/Modules/_struct.c
@@ -497,9 +497,8 @@ nu_ulonglong(_structmodulestate *state, const char *p, const formatdef *f)
static PyObject *
nu_bool(_structmodulestate *state, const char *p, const formatdef *f)
{
- _Bool x;
- memcpy(&x, p, sizeof x);
- return PyBool_FromLong(x != 0);
+ const _Bool bool_false = 0;
+ return PyBool_FromLong(memcmp(p, &bool_false, sizeof(_Bool)));
}