]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-135321: Always raise a correct exception for BINSTRING argument > 0x7fffffff in...
authorJustin Applegate <70449145+Legoclones@users.noreply.github.com>
Wed, 11 Jun 2025 10:15:12 +0000 (06:15 -0400)
committerGitHub <noreply@github.com>
Wed, 11 Jun 2025 10:15:12 +0000 (10:15 +0000)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/test/pickletester.py
Misc/NEWS.d/next/Library/2025-06-10-00-42-30.gh-issue-135321.UHh9jT.rst [new file with mode: 0644]
Modules/_pickle.c

index 9d6ae3e4d00ece31bd88f70b96e9b66669515ebe..9a3a26a8400844eaf1fc8854855be84fa1b0fc63 100644 (file)
@@ -1100,6 +1100,11 @@ class AbstractUnpickleTests:
         self.check_unpickling_error((pickle.UnpicklingError, OverflowError),
                                     dumped)
 
+    def test_large_binstring(self):
+        errmsg = 'BINSTRING pickle has negative byte count'
+        with self.assertRaisesRegex(pickle.UnpicklingError, errmsg):
+            self.loads(b'T\0\0\0\x80')
+
     def test_get(self):
         pickled = b'((lp100000\ng100000\nt.'
         unpickled = self.loads(pickled)
diff --git a/Misc/NEWS.d/next/Library/2025-06-10-00-42-30.gh-issue-135321.UHh9jT.rst b/Misc/NEWS.d/next/Library/2025-06-10-00-42-30.gh-issue-135321.UHh9jT.rst
new file mode 100644 (file)
index 0000000..9e63d8e
--- /dev/null
@@ -0,0 +1 @@
+Raise a correct exception for values greater than 0x7fffffff for the ``BINSTRING`` opcode in the C implementation of :mod:`pickle`.
index 86d8b38620cb7fdffac4c2604e5baeefde84ebaf..cf3ceb43fb3f3fe838ca13aa312a1f53026b5d29 100644 (file)
@@ -5543,17 +5543,16 @@ static int
 load_counted_binstring(PickleState *st, UnpicklerObject *self, int nbytes)
 {
     PyObject *obj;
-    Py_ssize_t size;
+    long size;
     char *s;
 
     if (_Unpickler_Read(self, st, &s, nbytes) < 0)
         return -1;
 
-    size = calc_binsize(s, nbytes);
+    size = calc_binint(s, nbytes);
     if (size < 0) {
-        PyErr_Format(st->UnpicklingError,
-                     "BINSTRING exceeds system's maximum size of %zd bytes",
-                     PY_SSIZE_T_MAX);
+        PyErr_SetString(st->UnpicklingError,
+                     "BINSTRING pickle has negative byte count");
         return -1;
     }