diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-03-07 13:55:27 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2024-03-07 16:39:22 +0000 |
commit | 7bc27c4d62af36b74f445e213204cdd89d1fc771 (patch) | |
tree | 022f32cf4b64fd443a3ce1ba01d472868a2dcc52 | |
parent | 79f96174d646423e2b42998e14b6c27bb4120a76 (diff) |
Fix a crash when using struct.unpack() on a QByteArray with Limited API
Unconditionally setting view->strides on the Py_Buffer causes a crash.
Update the code in our copy of PyBuffer_FillInfo() from CPython.
Fixes: PYSIDE-2628
Pick-to: 6.5
Change-Id: I6f244090a65442003cecfce70c6f8164b41ba99a
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
(cherry picked from commit 25e993273f7fc7445e5ccd1f446fe484a9c51d02)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | sources/pyside6/PySide6/glue/qtcore.cpp | 4 | ||||
-rw-r--r-- | sources/pyside6/tests/QtCore/qbytearray_test.py | 8 |
2 files changed, 10 insertions, 2 deletions
diff --git a/sources/pyside6/PySide6/glue/qtcore.cpp b/sources/pyside6/PySide6/glue/qtcore.cpp index 1759fc044..4d755606c 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -802,10 +802,10 @@ static int SbkQByteArray_getbufferproc(PyObject *obj, Py_buffer *view, int flags view->len = cppSelf->size(); view->readonly = 0; view->itemsize = 1; - view->format = const_cast<char *>("c"); + view->format = (flags & PyBUF_FORMAT) == PyBUF_FORMAT ? const_cast<char *>("B") : nullptr; view->ndim = 1; view->shape = (flags & PyBUF_ND) == PyBUF_ND ? &(view->len) : nullptr; - view->strides = &view->itemsize; + view->strides = (flags & PyBUF_STRIDES) == PyBUF_STRIDES ? &(view->itemsize) : nullptr; view->suboffsets = nullptr; view->internal = nullptr; diff --git a/sources/pyside6/tests/QtCore/qbytearray_test.py b/sources/pyside6/tests/QtCore/qbytearray_test.py index c00674859..1a870fcde 100644 --- a/sources/pyside6/tests/QtCore/qbytearray_test.py +++ b/sources/pyside6/tests/QtCore/qbytearray_test.py @@ -6,6 +6,7 @@ import ctypes import os import pickle +import struct import sys import unittest @@ -255,6 +256,13 @@ class QByteArraySliceAssignment(unittest.TestCase): actual_bytes = bytes(byte_array) self.assertEqual(orig_bytes, actual_bytes) + def testUnpack(self): + b = QByteArray(b'\x19\x00\x00\x00\xc4\t\x00\x00') + t = struct.unpack('<ii', b) + self.assertEqual(len(t), 2) + self.assertEqual(t[0], 25) + self.assertEqual(t[1], 2500) + if __name__ == '__main__': unittest.main() |