From ee451671cd0f54ec0d3ed84edcc7186858559924 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 25 Sep 2021 11:29:49 +0200 Subject: [PATCH] Util: Fix reading of file descriptors longer than 4096 bytes --- src/util/system.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/util/system.cpp b/src/util/system.cpp index b8c1246..7d62a9c 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -1,6 +1,6 @@ #include // errno, EAGAIN, EINTR #include // size_t -#include // perror +#include // perror, ssize_t #include // exit, WEXITSTATUS #include // strcpy, strtok #include // function @@ -228,13 +228,23 @@ void System::readFromFileDescriptor(int fileDescriptor[2], std::string& output) constexpr int bufferSize = 4096; char buffer[bufferSize]; - do { + for (;;) { const ssize_t result = read(fileDescriptor[ReadFileDescriptor], buffer, bufferSize); - // FIXME: also handle failure cases if (result > 0) { output.append(buffer, result); } - } while (errno == EAGAIN || errno == EINTR); + // EOF + if (result == 0) { + break; + } + // Error + else if (result == -1) { + if (errno != EAGAIN && errno != EINTR) { + perror("\033[31;1mError:\033[0m read"); + break; + } + } + } close(fileDescriptor[ReadFileDescriptor]); }