mirror of
https://github.com/python/cpython.git
synced 2025-08-19 08:11:46 +00:00
[3.12] gh-126594: Fix typeobject.c wrap_buffer() cast (GH-126754) (#127005)
gh-126594: Fix typeobject.c wrap_buffer() cast (GH-126754)
Reject flags smaller than INT_MIN.
(cherry picked from commit 84f07c3a4c
)
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
126acc1bb0
commit
9345dc165c
2 changed files with 18 additions and 3 deletions
|
@ -4442,6 +4442,21 @@ class TestBufferProtocol(unittest.TestCase):
|
||||||
self.assertEqual(_testcapi.PyBuffer_SizeFromFormat(format),
|
self.assertEqual(_testcapi.PyBuffer_SizeFromFormat(format),
|
||||||
struct.calcsize(format))
|
struct.calcsize(format))
|
||||||
|
|
||||||
|
@support.cpython_only
|
||||||
|
def test_flags_overflow(self):
|
||||||
|
# gh-126594: Check for integer overlow on large flags
|
||||||
|
try:
|
||||||
|
from _testcapi import INT_MIN, INT_MAX
|
||||||
|
except ImportError:
|
||||||
|
INT_MIN = -(2 ** 31)
|
||||||
|
INT_MAX = 2 ** 31 - 1
|
||||||
|
|
||||||
|
obj = b'abc'
|
||||||
|
for flags in (INT_MIN - 1, INT_MAX + 1):
|
||||||
|
with self.subTest(flags=flags):
|
||||||
|
with self.assertRaises(OverflowError):
|
||||||
|
obj.__buffer__(flags)
|
||||||
|
|
||||||
|
|
||||||
class TestPythonBufferProtocol(unittest.TestCase):
|
class TestPythonBufferProtocol(unittest.TestCase):
|
||||||
def test_basic(self):
|
def test_basic(self):
|
||||||
|
|
|
@ -8207,13 +8207,13 @@ wrap_buffer(PyObject *self, PyObject *args, void *wrapped)
|
||||||
if (flags == -1 && PyErr_Occurred()) {
|
if (flags == -1 && PyErr_Occurred()) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (flags > INT_MAX) {
|
if (flags > INT_MAX || flags < INT_MIN) {
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
"buffer flags too large");
|
"buffer flags out of range");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _PyMemoryView_FromBufferProc(self, Py_SAFE_DOWNCAST(flags, Py_ssize_t, int),
|
return _PyMemoryView_FromBufferProc(self, (int)flags,
|
||||||
(getbufferproc)wrapped);
|
(getbufferproc)wrapped);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue