Skip to content

Commit

Permalink
fix bad logic in windows console handler
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ab9rf committed Feb 17, 2025
1 parent c138083 commit bed6458
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions library/Console-windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE.
*/


#define NOMINMAX
#include <windows.h>
#include <conio.h>
#include <stdarg.h>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bed6458

Please sign in to comment.