From bed64583b05293452af78dc3c335b2225ad91666 Mon Sep 17 00:00:00 2001 From: Kelly Kinkade Date: Sun, 16 Feb 2025 19:05:21 -0600 Subject: [PATCH] fix bad logic in windows console handler prompt refresh logic would underflow and infinite-loop if window size was smaller than the prompt size replaced with direct math instead of while loops --- library/Console-windows.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/library/Console-windows.cpp b/library/Console-windows.cpp index 85934b2f37..83ed0f64ee 100644 --- a/library/Console-windows.cpp +++ b/library/Console-windows.cpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. */ +#define NOMINMAX #include #include #include @@ -232,16 +233,13 @@ namespace DFHack size_t len = raw_buffer.size(); int cooked_cursor = raw_cursor; - while ((plen + cooked_cursor) >= cols) - { - buf++; - len--; - cooked_cursor--; - } - while (plen + len > cols) - { - len--; - } + int adj = std::min(plen + cooked_cursor - cols, len); + buf += adj; + len -= adj; + cooked_cursor -= adj; + + int adj2 = std::min(plen + len - cols, len); + len -= adj2; CONSOLE_SCREEN_BUFFER_INFO inf = { 0 }; GetConsoleScreenBufferInfo(console_out, &inf);