You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_imp.get_frozen_object possible incorrect use of PyBUF_READ #114685
PyObject_GetBuffer() expects a numeric flag, but it uses bitflag model with example logic:
if (!REQ_FORMAT(flags)) {
/* NULL indicates that the buffer's data type has been cast to 'B'. view->itemsize is the _previous_ itemsize. If shape is present, the equality product(shape) * itemsize = len still holds at this point. The equality calcsize(format) = itemsize does _not_ hold from here on! */view->format=NULL;
}
if (REQ_C_CONTIGUOUS(flags) && !MV_C_CONTIGUOUS(baseflags)) {
PyErr_SetString(PyExc_BufferError,
"memoryview: underlying buffer is not C-contiguous");
return-1;
}
if (REQ_F_CONTIGUOUS(flags) && !MV_F_CONTIGUOUS(baseflags)) {
PyErr_SetString(PyExc_BufferError,
"memoryview: underlying buffer is not Fortran contiguous");
return-1;
}
if (REQ_ANY_CONTIGUOUS(flags) && !MV_ANY_CONTIGUOUS(baseflags)) {
PyErr_SetString(PyExc_BufferError,
"memoryview: underlying buffer is not contiguous");
return-1;
}
PyBYF_READ is equal to 256, so it just contains all the features. PyBUF_SIMPLE is just 0, so no extras.
Unfortunately the value of PyBUF_READ intersects with PyBUF_INDIRECT, PyBUF_FULL and PyBUF_FULL_RO, so we cannot just ban this bit. But fortunately other values has other bits set, so this error is still distinguishable.
What about PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int flags)? Do we need to check flags there as well?
Activity
pythongh-114685: Fix incorrect use of `PyBUF_READ` in `import.c`
serhiy-storchaka commentedon Jan 28, 2024
Other question, why it was not caught in tests? Does
PyObject_GetBuffer()ignore unknown flags or this code was never executed?sobolevn commentedon Jan 29, 2024
PyObject_GetBuffer()expects a numeric flag, but it uses bitflag model with example logic:PyBYF_READis equal to 256, so it just contains all the features.PyBUF_SIMPLEis just 0, so no extras.gh-114685: Fix incorrect use of PyBUF_READ in import.c (GH-114686)
pythongh-114685: Fix incorrect use of PyBUF_READ in import.c (pythonG…
pythongh-114685: Fix incorrect use of PyBUF_READ in import.c (pythonG…
[3.11] gh-114685: Fix incorrect use of PyBUF_READ in import.c (GH-114686
[3.12] gh-114685: Fix incorrect use of PyBUF_READ in import.c (GH-114686
pythongh-114685: Check flags in PyObject_GetBuffer()
gh-114685: Check flags in PyObject_GetBuffer() (GH-114707)
serhiy-storchaka commentedon Jan 31, 2024
Unfortunately the value of PyBUF_READ intersects with PyBUF_INDIRECT, PyBUF_FULL and PyBUF_FULL_RO, so we cannot just ban this bit. But fortunately other values has other bits set, so this error is still distinguishable.
sobolevn commentedon Jan 31, 2024
What about
PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, int readonly, int flags)? Do we need to checkflagsthere as well?serhiy-storchaka commentedon Jan 31, 2024
Maybe. We do not have examples of the misuse in this case, and it is much less used, but a similar error is possible here too.
sobolevn commentedon Jan 31, 2024
I will send an example PR, so we can decide 👍
pythongh-114685: `PyBuffer_FillInfo` now raises on `PyBUF_{READ,WRITE}`
pythongh-114685: `PyBuffer_FillInfo` now raises on `PyBUF_{READ,WRITE}`
gh-114685: PyBuffer_FillInfo() now raises on PyBUF_{READ,WRITE} (GH-1…
pythongh-114685: Fix incorrect use of PyBUF_READ in import.c (pythonG…
pythongh-114685: Check flags in PyObject_GetBuffer() (pythonGH-114707)
pythongh-114685: PyBuffer_FillInfo() now raises on PyBUF_{READ,WRITE} (…
pythongh-114685: PyBuffer_FillInfo() now raises on PyBUF_{READ,WRITE} (…