From 4532c2c489d58abd27adb75aa6f86bd788105b33 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 11 Mar 2019 00:06:44 +0100 Subject: [PATCH] - fixed crash when inserting CJK text. --- src/c_console.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/c_console.cpp b/src/c_console.cpp index cdaa47912..aecaf3c50 100644 --- a/src/c_console.cpp +++ b/src/c_console.cpp @@ -249,28 +249,30 @@ public: } - unsigned CharsForCells(unsigned cells) + unsigned CharsForCells(unsigned cellin, bool *overflow) { unsigned chars = 0; + int cells = cellin; while (cells > 0) { int w; NewConsoleFont->GetChar(Text[chars++], CR_UNTRANSLATED, &w); cells -= w / 9; } - return cells == 0? chars : -chars; + *overflow = (cells < 0); + return cells; } void MakeStartPosGood() { // Make sure both values point to something valid. - if (CursorPos > Text.length()) CursorPos = Text.length(); - if (StartPos > Text.length()) StartPos = Text.length(); + if (CursorPos > Text.length()) CursorPos = (unsigned)Text.length(); + if (StartPos > Text.length()) StartPos = (unsigned)Text.length(); CursorPosCells = CalcCellSize(CursorPos); StartPosCells = CalcCellSize(StartPos); - unsigned LengthCells = CalcCellSize(Text.length()); + unsigned LengthCells = CalcCellSize((unsigned)Text.length()); int n = StartPosCells; unsigned cols = ConCols / active_con_scale(); @@ -288,11 +290,12 @@ public: n = CursorPosCells; } StartPosCells = MAX(0, n); - StartPos = CharsForCells(StartPosCells); - if (StartPos < 0) + bool overflow; + StartPos = CharsForCells(StartPosCells, &overflow); + if (overflow) { // We ended up in the middle of a double cell character, so set the start to the following character. - StartPos = -StartPos + 1; + StartPos++; StartPosCells++; } }