diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-03-07 13:55:27 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2024-03-07 15:46:13 +0100 |
commit | 25e993273f7fc7445e5ccd1f446fe484a9c51d02 (patch) | |
tree | c7f85e868273754f6043b772b65f655e4c0ef162 | |
parent | 5829f83532cda088c5f7a31070563840d3617113 (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.6 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>
-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 bddbfab53..3ea3c6ee7 100644 --- a/sources/pyside6/PySide6/glue/qtcore.cpp +++ b/sources/pyside6/PySide6/glue/qtcore.cpp @@ -789,10 +789,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 555faf1c9..cb8f9a431 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 @@ -256,6 +257,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) + class QCompressTest(unittest.TestCase): def testQByteArrayCompression(self): |