Skip to content

Commit fd276ad

Browse files
miss-islingtonvstinnerJelleZijlstra
authored
[3.13] gh-126594: Fix typeobject.c wrap_buffer() cast (GH-126754) (#127004)
gh-126594: Fix typeobject.c wrap_buffer() cast (GH-126754) Reject flags smaller than INT_MIN. (cherry picked from commit 84f07c3) Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
1 parent 190d710 commit fd276ad

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Diff for: Lib/test/test_buffer.py

+15
Original file line numberDiff line numberDiff line change
@@ -4446,6 +4446,21 @@ def test_pybuffer_size_from_format(self):
44464446
self.assertEqual(_testcapi.PyBuffer_SizeFromFormat(format),
44474447
struct.calcsize(format))
44484448

4449+
@support.cpython_only
4450+
def test_flags_overflow(self):
4451+
# gh-126594: Check for integer overlow on large flags
4452+
try:
4453+
from _testcapi import INT_MIN, INT_MAX
4454+
except ImportError:
4455+
INT_MIN = -(2 ** 31)
4456+
INT_MAX = 2 ** 31 - 1
4457+
4458+
obj = b'abc'
4459+
for flags in (INT_MIN - 1, INT_MAX + 1):
4460+
with self.subTest(flags=flags):
4461+
with self.assertRaises(OverflowError):
4462+
obj.__buffer__(flags)
4463+
44494464

44504465
class TestPythonBufferProtocol(unittest.TestCase):
44514466
def test_basic(self):

Diff for: Objects/typeobject.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -8952,13 +8952,13 @@ wrap_buffer(PyObject *self, PyObject *args, void *wrapped)
89528952
if (flags == -1 && PyErr_Occurred()) {
89538953
return NULL;
89548954
}
8955-
if (flags > INT_MAX) {
8955+
if (flags > INT_MAX || flags < INT_MIN) {
89568956
PyErr_SetString(PyExc_OverflowError,
8957-
"buffer flags too large");
8957+
"buffer flags out of range");
89588958
return NULL;
89598959
}
89608960

8961-
return _PyMemoryView_FromBufferProc(self, Py_SAFE_DOWNCAST(flags, Py_ssize_t, int),
8961+
return _PyMemoryView_FromBufferProc(self, (int)flags,
89628962
(getbufferproc)wrapped);
89638963
}
89648964

0 commit comments

Comments
 (0)