Details
Description
TLDR: feeding a `QByteArray` to `struct.unpack()` causes a segfault. To reproduce:
>>> import struct >>> from PySide6.QtCore import QByteArray >>> struct.unpack('<ii', b'\x19\x00\x00\x00\xc4\t\x00\x00') (25, 2500) >>> b = QByteArray(b'\x19\x00\x00\x00\xc4\t\x00\x00') >>> struct.unpack('<ii', b) Segmentation fault (core dumped)
The longer story:
A project previously used PyQt5 and is being migrated to PySide6. The code previously used to read data from `QTcpSocket.read()` and parse the bytes with `struct.unpack()`. After the upgrade, this crash emerged.
It's a pretty easy fix, the `QByteArray` can just be converted to a normal Python `bytes` object right after the `QTcpSocket.read()`.
I'm not sure exactly where the bug is... `struct.unpack()` should raise an exception if it gets a type it can't deal with. For example:
>>> import struct >>> from PySide6.QtCore import QObject, QByteArray >>> struct.unpack('<ii', QObject()) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a bytes-like object is required, not 'PySide6.QtCore.QObject'
So, maybe that's a Python `struct` problem? But then I wonder, if the Python `bytes` class is more or less equivalent to the `QByteArray` class, should `QIODevice.read()` return `bytes` instead of `QByteArray` in PySide? Does `QByteArray` not behave as a "bytes-like object" under all conditions? Not sure, but it's easy to reproduce. Let me know if anything else is needed.
Attachments
For Gerrit Dashboard: PYSIDE-2628 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
546120,5 | libpyside: Add a debug operator for a Py_Buffer | dev | pyside/pyside-setup | Status: MERGED | +2 | 0 |
546121,3 | Fix a crash when using struct.unpack() on a QByteArray with Limited API | dev | pyside/pyside-setup | Status: MERGED | -1 | 0 |
546201,2 | Fix a crash when using struct.unpack() on a QByteArray with Limited API | 6.6 | pyside/pyside-setup | Status: MERGED | -1 | 0 |
546255,2 | Fix a crash when using struct.unpack() on a QByteArray with Limited API | tqtc/lts-6.5 | pyside/tqtc-pyside-setup | Status: MERGED | -1 | 0 |