From 2ff4a69f53d498ee037d05ba028e3117d0802cba Mon Sep 17 00:00:00 2001 From: nothingTVatYT Date: Mon, 9 Oct 2023 17:25:28 +0200 Subject: [PATCH] fix reading of pipe buffer from external filechooser --- .../Engine/Platform/Linux/LinuxFileSystem.cpp | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp index 279813470..3f33a8f65 100644 --- a/Source/Engine/Platform/Linux/LinuxFileSystem.cpp +++ b/Source/Engine/Platform/Linux/LinuxFileSystem.cpp @@ -69,7 +69,30 @@ bool LinuxFileSystem::ShowOpenFileDialog(Window* parentWindow, const StringView& } FILE* f = popen(cmd, "r"); char buf[2048]; - fgets(buf, ARRAY_COUNT(buf), f); + char* writePointer = buf; + int remainingCapacity = ARRAY_COUNT(buf); + // make sure we read all output from kdialog + while (remainingCapacity > 0 && fgets(writePointer, remainingCapacity, f)) + { + int r = strlen(writePointer); + writePointer += r; + remainingCapacity -= r; + } + if (remainingCapacity <= 0) + { + LOG(Error, "You selected more files than an internal buffer can hold. Try selecting fewer files at a time."); + // in case of an overflow we miss the closing null byte, add it after the rightmost linefeed + while (*writePointer != '\n') + { + writePointer--; + if (writePointer == buf) + { + *buf = 0; + break; + } + } + *(++writePointer) = 0; + } int result = pclose(f); if (result != 0) {