UnixPtyProcess: Fix read loop

Previously a value of "-1" from ::read would result in an endless loop.
This could easily be reproduced by "cat /dev/random | base64"

The buffer usage was also much more complicated than needed.
A static readBuffer now keeps the amount of allocations lower.

Change-Id: I5bb1a3c84b107ff8c2d3801bca8c6ae9a709cdb3
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-03-03 09:00:11 +01:00
parent b26e3a4501
commit 6d70a27965

View File

@@ -164,19 +164,14 @@ bool UnixPtyProcess::startProcess(const QString &shellPath,
QObject::connect(m_readMasterNotify, &QSocketNotifier::activated, [this](int socket) { QObject::connect(m_readMasterNotify, &QSocketNotifier::activated, [this](int socket) {
Q_UNUSED(socket) Q_UNUSED(socket)
QByteArray buffer; const size_t maxRead = 16 * 1024;
int size = 1025; static std::array<char, maxRead> buffer;
int readSize = 1024;
QByteArray data;
do {
char nativeBuffer[size];
int len = ::read(m_shellProcess.m_handleMaster, nativeBuffer, readSize);
data = QByteArray(nativeBuffer, len);
buffer.append(data);
} while (data.size() == readSize); //last data block always < readSize
m_shellReadBuffer.append(buffer); int len = ::read(m_shellProcess.m_handleMaster, buffer.data(), buffer.size());
if (len > 0) {
m_shellReadBuffer.append(buffer.data(), len);
m_shellProcess.emitReadyRead(); m_shellProcess.emitReadyRead();
}
}); });
QObject::connect(&m_shellProcess, &QProcess::finished, &m_shellProcess, [this](int exitCode) { QObject::connect(&m_shellProcess, &QProcess::finished, &m_shellProcess, [this](int exitCode) {