From 036edc24cb73f9881d978c8c68b4f19be3bbab2e Mon Sep 17 00:00:00 2001 From: Alexey Min Date: Mon, 1 Feb 2021 17:26:32 +0300 Subject: [PATCH] Use loop instead of recursion This fixes crash when clicking "screenshot" button in the top panel on postmarketOS. The reason for crash is that default thread stack size in musl libc is limited, and recursion in a function with a 4 Kb buffer consumes it all. --- containments/panel/phonepanel.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/containments/panel/phonepanel.cpp b/containments/panel/phonepanel.cpp index 91c3fe53..c76b1816 100644 --- a/containments/panel/phonepanel.cpp +++ b/containments/panel/phonepanel.cpp @@ -46,20 +46,22 @@ static int readData(int theFile, QByteArray &theDataOut) char lBuffer[4096]; int lRetryCount = 0; ssize_t lBytesRead = 0; - while (true) { - lBytesRead = QT_READ(theFile, lBuffer, sizeof lBuffer); - // give user 30 sec to click a window, afterwards considered as error - if (lBytesRead == -1 && (errno == EAGAIN) && ++lRetryCount < 30000) { - usleep(1000); - } else { - break; - } - } - if (lBytesRead > 0) { - theDataOut.append(lBuffer, lBytesRead); - lBytesRead = readData(theFile, theDataOut); - } + do { + // give user 30 sec to click a window, afterwards considered as error + while (true) { + lBytesRead = QT_READ(theFile, lBuffer, sizeof lBuffer); + if (lBytesRead == -1 && (errno == EAGAIN) && ++lRetryCount < 30000) { + usleep(1000); + } else { + break; + } + } + + if (lBytesRead > 0) { + theDataOut.append(lBuffer, lBytesRead); + } + } while (lBytesRead > 0); return lBytesRead; }