Skip to content

Commit 0e969fe

Browse files
miss-islingtonLegoclonesserhiy-storchaka
authored
[3.14] gh-135321: Always raise a correct exception for BINSTRING argument > 0x7fffffff in pickle (GH-135322) (GH-135382)
(cherry picked from commit 2b8b477) Co-authored-by: Justin Applegate <70449145+Legoclones@users.noreply.github.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 30494cc commit 0e969fe

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

Lib/test/pickletester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,11 @@ def test_large_32b_binunicode8(self):
11001100
self.check_unpickling_error((pickle.UnpicklingError, OverflowError),
11011101
dumped)
11021102

1103+
def test_large_binstring(self):
1104+
errmsg = 'BINSTRING pickle has negative byte count'
1105+
with self.assertRaisesRegex(pickle.UnpicklingError, errmsg):
1106+
self.loads(b'T\0\0\0\x80')
1107+
11031108
def test_get(self):
11041109
pickled = b'((lp100000\ng100000\nt.'
11051110
unpickled = self.loads(pickled)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise a correct exception for values greater than 0x7fffffff for the ``BINSTRING`` opcode in the C implementation of :mod:`pickle`.

Modules/_pickle.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5539,17 +5539,16 @@ static int
55395539
load_counted_binstring(PickleState *st, UnpicklerObject *self, int nbytes)
55405540
{
55415541
PyObject *obj;
5542-
Py_ssize_t size;
5542+
long size;
55435543
char *s;
55445544

55455545
if (_Unpickler_Read(self, st, &s, nbytes) < 0)
55465546
return -1;
55475547

5548-
size = calc_binsize(s, nbytes);
5548+
size = calc_binint(s, nbytes);
55495549
if (size < 0) {
5550-
PyErr_Format(st->UnpicklingError,
5551-
"BINSTRING exceeds system's maximum size of %zd bytes",
5552-
PY_SSIZE_T_MAX);
5550+
PyErr_SetString(st->UnpicklingError,
5551+
"BINSTRING pickle has negative byte count");
55535552
return -1;
55545553
}
55555554

0 commit comments

Comments
 (0)