From 7bc27c4d62af36b74f445e213204cdd89d1fc771 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Thu, 7 Mar 2024 13:55:27 +0100 Subject: 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 Reviewed-by: Cristian Maureira-Fredes (cherry picked from commit 25e993273f7fc7445e5ccd1f446fe484a9c51d02) Reviewed-by: Qt Cherry-pick Bot --- sources/pyside6/PySide6/glue/qtcore.cpp | 4 ++-- 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("c"); + view->format = (flags & PyBUF_FORMAT) == PyBUF_FORMAT ? const_cast("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('