Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-2628

Segfault when using a QByteArray in place of a Python byte array (Limited API)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P2: Important
    • 6.7.0, 6.6.3, 6.5.6
    • 6.5.1, 6.6.2
    • PySide
    • None
    • Python 3.10 on Ubuntu 20.04 and Python 3.11 on Arch
    • Linux/X11
    • 25e993273 (dev), 7bc27c4d6 (6.6), a2b621bac (tqtc/lts-6.5), 109030984 (dev)

    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

        1. pyside2628_diag.diff
          3 kB
        2. pyside2628_log.txt
          1 kB
        3. pyside2628_pyqt.py
          0.5 kB
        4. pyside2628_stack_win.txt
          6 kB
        5. pyside2628.py
          0.5 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kleint Friedemann Kleint
            malocascio Mark Locascio
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews