Windows: fix readyRead() signal emission


The QIODevice::readyRead() and QIODevice::waitForReadyRead() docs both
mention that the readyRead() signal cannot be emitted recursively.
If we connect a slot to the signal, and call waitForReadyRead() in this
slot, the expected behavior would be that we do not receive more
readyRead() signals until we exit the slot.

However, the QSerialPort Windows implementation did not follow the
docs. That could lead to a crash due to a stack overflow, because the
connected slot could be entered recursively.

A bit of git log research reveals the following:
* Originally, SerialPortPrivate::canReadNotification() method used the
  emittedReadyRead flag to guard against the recursion. The code was
  common for Unix and Windows implementations.
* Later, commit 1816554b1f2ef3bb70a408b756f3eba78070df56 dropped the
  SerialPortEngine helper class and instead introduced the split
  between Unix and Windows SerialPortPrivate implementations.
  The Unix implementaiton inherited the common logic, and kept the
  proper handling of the flag.
  The Windows implementation introduced the new readyReadEmitted flag,
  but didn't use it correctly. As a result, the recursion guard was
  lost.
* After several refactoring steps, the unused readyReadEmitted flag
  was removed in 9a9aed53e1e23200d471ced91a83fbca9976f231.

This patch moves the emittedReadyRead flag from unix-specific to common
section of QSerialPortPrivate, and adjusts the Windows-specific method
QSerialPortPrivate::emitReadyRead() to make use of this flag, similarly
to the Unix implementation.

Added a unit-tests that makes sure that the readyRead() signal is not
emitted recursively. Tested on Windows, Linux, and macOS.

[ChangeLog][QSerialPort][Windows] Fixed a bug where the readyRead()
signal did not follow the QIODevice docs and could be emitted
recursively.

Fixes: QTBUG-84689
Pick-to: 6.9 6.8 6.5
Change-Id: I3d525fdace3d73f58ada86d3e1b6543741ca6aa7
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
commit3f05f97ded0bf4c82f2e6e8c41f6e8974a6409c2
parent1f1a41b61c37dc0fc586e00fcab05181d66cf24f
authorIvan Solovev <ivan.solovev@qt.io>
committerIvan Solovev <ivan.solovev@qt.io>
commit timeWed, 11 Dec 2024 11:56:20 +0100