]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-138739) (#138782)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 12 Sep 2025 13:22:43 +0000 (15:22 +0200)
committerGitHub <noreply@github.com>
Fri, 12 Sep 2025 13:22:43 +0000 (16:22 +0300)
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/test_long.py
Misc/NEWS.d/next/Core_and_Builtins/2025-09-10-14-53-59.gh-issue-71810.ppf0J-.rst [new file with mode: 0644]
Objects/longobject.c

index d63bc19ed9c9a2436a1c8dfcf31ccf87c5decfd1..b48a8812a1a2d13fb5499210bf19ea4e82799a04 100644 (file)
@@ -1374,17 +1374,22 @@ class LongTest(unittest.TestCase):
         check(tests4, 'little', signed=False)
 
         self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=False)
-        self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=True)
         self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=False)
-        self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=True)
+        self.assertRaises(OverflowError, (128).to_bytes, 1, 'big', signed=True)
+        self.assertRaises(OverflowError, (128).to_bytes, 1, 'little', signed=True)
+        self.assertRaises(OverflowError, (-129).to_bytes, 1, 'big', signed=True)
+        self.assertRaises(OverflowError, (-129).to_bytes, 1, 'little', signed=True)
         self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False)
         self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False)
         self.assertEqual((0).to_bytes(0, 'big'), b'')
+        self.assertEqual((0).to_bytes(0, 'big', signed=True), b'')
         self.assertEqual((1).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x01')
         self.assertEqual((0).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x00')
         self.assertEqual((-1).to_bytes(5, 'big', signed=True),
                          b'\xff\xff\xff\xff\xff')
         self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')
+        self.assertRaises(OverflowError, (-1).to_bytes, 0, 'big', signed=True)
+        self.assertRaises(OverflowError, (-1).to_bytes, 0, 'little', signed=True)
 
         # gh-98783
         class SubStr(str):
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-10-14-53-59.gh-issue-71810.ppf0J-.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-10-14-53-59.gh-issue-71810.ppf0J-.rst
new file mode 100644 (file)
index 0000000..a87db44
--- /dev/null
@@ -0,0 +1,2 @@
+Raise :exc:`OverflowError` for ``(-1).to_bytes()`` for signed conversions
+when bytes count is zero.  Patch by Sergey B Kirpichev.
index 2b533312fee67347f291cadf11925a45dc438fda..cc061ffbe6b24e0c7707b6d5938ee1cef9b72b52 100644 (file)
@@ -1138,7 +1138,7 @@ _PyLong_AsByteArray(PyLongObject* v,
         *p = (unsigned char)(accum & 0xff);
         p += pincr;
     }
-    else if (j == n && n > 0 && is_signed) {
+    else if (j == n && is_signed) {
         /* The main loop filled the byte array exactly, so the code
            just above didn't get to ensure there's a sign bit, and the
            loop below wouldn't add one either.  Make sure a sign bit