Skip to content

Commit 2b8b477

Browse files
gh-135321: Always raise a correct exception for BINSTRING argument > 0x7fffffff in pickle (GH-135322)
Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 5ae669f commit 2b8b477

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
@@ -5543,17 +5543,16 @@ static int
55435543
load_counted_binstring(PickleState *st, UnpicklerObject *self, int nbytes)
55445544
{
55455545
PyObject *obj;
5546-
Py_ssize_t size;
5546+
long size;
55475547
char *s;
55485548

55495549
if (_Unpickler_Read(self, st, &s, nbytes) < 0)
55505550
return -1;
55515551

5552-
size = calc_binsize(s, nbytes);
5552+
size = calc_binint(s, nbytes);
55535553
if (size < 0) {
5554-
PyErr_Format(st->UnpicklingError,
5555-
"BINSTRING exceeds system's maximum size of %zd bytes",
5556-
PY_SSIZE_T_MAX);
5554+
PyErr_SetString(st->UnpicklingError,
5555+
"BINSTRING pickle has negative byte count");
55575556
return -1;
55585557
}
55595558

0 commit comments

Comments
 (0)