File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +18
-3
lines changed
Original file line number Diff line number Diff line change @@ -4442,6 +4442,21 @@ def test_pybuffer_size_from_format(self):
4442
4442
self .assertEqual (_testcapi .PyBuffer_SizeFromFormat (format ),
4443
4443
struct .calcsize (format ))
4444
4444
4445
+ @support .cpython_only
4446
+ def test_flags_overflow (self ):
4447
+ # gh-126594: Check for integer overlow on large flags
4448
+ try :
4449
+ from _testcapi import INT_MIN , INT_MAX
4450
+ except ImportError :
4451
+ INT_MIN = - (2 ** 31 )
4452
+ INT_MAX = 2 ** 31 - 1
4453
+
4454
+ obj = b'abc'
4455
+ for flags in (INT_MIN - 1 , INT_MAX + 1 ):
4456
+ with self .subTest (flags = flags ):
4457
+ with self .assertRaises (OverflowError ):
4458
+ obj .__buffer__ (flags )
4459
+
4445
4460
4446
4461
class TestPythonBufferProtocol (unittest .TestCase ):
4447
4462
def test_basic (self ):
Original file line number Diff line number Diff line change @@ -8207,13 +8207,13 @@ wrap_buffer(PyObject *self, PyObject *args, void *wrapped)
8207
8207
if (flags == -1 && PyErr_Occurred ()) {
8208
8208
return NULL ;
8209
8209
}
8210
- if (flags > INT_MAX ) {
8210
+ if (flags > INT_MAX || flags < INT_MIN ) {
8211
8211
PyErr_SetString (PyExc_OverflowError ,
8212
- "buffer flags too large " );
8212
+ "buffer flags out of range " );
8213
8213
return NULL ;
8214
8214
}
8215
8215
8216
- return _PyMemoryView_FromBufferProc (self , Py_SAFE_DOWNCAST ( flags , Py_ssize_t , int ),
8216
+ return _PyMemoryView_FromBufferProc (self , ( int )flags ,
8217
8217
(getbufferproc )wrapped );
8218
8218
}
8219
8219
You can’t perform that action at this time.
0 commit comments