From 6d70a2796564dae8d8cec591dfe0d7b2f58d0469 Mon Sep 17 00:00:00 2001 From: Marcus Tillmanns Date: Fri, 3 Mar 2023 09:00:11 +0100 Subject: [PATCH] UnixPtyProcess: Fix read loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/libs/3rdparty/libptyqt/unixptyprocess.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/libs/3rdparty/libptyqt/unixptyprocess.cpp b/src/libs/3rdparty/libptyqt/unixptyprocess.cpp index e049a2abb92..8c018daf8c0 100644 --- a/src/libs/3rdparty/libptyqt/unixptyprocess.cpp +++ b/src/libs/3rdparty/libptyqt/unixptyprocess.cpp @@ -164,19 +164,14 @@ bool UnixPtyProcess::startProcess(const QString &shellPath, QObject::connect(m_readMasterNotify, &QSocketNotifier::activated, [this](int socket) { Q_UNUSED(socket) - QByteArray buffer; - int size = 1025; - 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 + const size_t maxRead = 16 * 1024; + static std::array buffer; - m_shellReadBuffer.append(buffer); - m_shellProcess.emitReadyRead(); + int len = ::read(m_shellProcess.m_handleMaster, buffer.data(), buffer.size()); + if (len > 0) { + m_shellReadBuffer.append(buffer.data(), len); + m_shellProcess.emitReadyRead(); + } }); QObject::connect(&m_shellProcess, &QProcess::finished, &m_shellProcess, [this](int exitCode) {