From 173ca3e58e226b0e9f115c42371c2cb13415b53c Mon Sep 17 00:00:00 2001 From: pryon Date: Sat, 21 Sep 2024 20:02:58 +0200 Subject: [PATCH] Fix assertion failures on `str` operations --- code/client/cl_uimaprunner.cpp | 2 +- code/uilib/uimledit.cpp | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/code/client/cl_uimaprunner.cpp b/code/client/cl_uimaprunner.cpp index 40b0730d..20f4fbb9 100644 --- a/code/client/cl_uimaprunner.cpp +++ b/code/client/cl_uimaprunner.cpp @@ -83,7 +83,7 @@ void PickFile(const char *name, Listener *obj, Event& event) if (name && *name && strchr(name, '/')) { currentpath = name; - for (i = currentpath.length(); i > 0; i--) { + for (i = currentpath.length() - 1; i > 0; i--) { if (currentpath[i] == '/') { break; } diff --git a/code/uilib/uimledit.cpp b/code/uilib/uimledit.cpp index 45463ab7..688e124f 100644 --- a/code/uilib/uimledit.cpp +++ b/code/uilib/uimledit.cpp @@ -646,12 +646,19 @@ void UIMultiLineEdit::DeleteSelection(void) for (i = 0, m_lines.IterateFromHead(); m_lines.IsCurrentValid() && i < topsel->line; i++, m_lines.IterateNext()) {} + // delete topmost line of the selection, but only up to topsel->column str& topline = m_lines.getCurrent(); - topline.CapLength(topsel->column); + if (topline.length() > topsel->column) { + topline.CapLength(topsel->column); + } m_lines.IterateNext(); - str& line = m_lines.getCurrent(); + str line = m_lines.getCurrent(); + + // merge remainder of topmost line with the remainder after the end of selection + if (line.length() > botsel->column) { + topline += &line[botsel->column]; + } - topline += &line[botsel->column]; m_lines.RemoveCurrentSetPrev(); *botsel = *topsel; m_vertscroll->setNumItems(m_lines.getCount()); @@ -691,8 +698,11 @@ void UIMultiLineEdit::CharEvent(int ch) m_changed = true; str& line = LineFromLineNumber(m_selection.begin.line, false); - str otherline = &line[m_selection.begin.column]; - otherline.CapLength(m_selection.begin.column); + str otherline = ""; + if (line.length() > m_selection.begin.column) { + otherline = &line[m_selection.begin.column]; + line.CapLength(m_selection.begin.column); + } if (m_lines.IsCurrentValid()) { m_lines.InsertAfterCurrent(otherline); @@ -730,7 +740,9 @@ void UIMultiLineEdit::CopySelection(void) line = LineFromLineNumber(topsel->line, true); - clipText += &line[topsel->column]; + if (line.length() > topsel->column) { + clipText += &line[topsel->column]; + } if (topsel->line == botsel->line) { clipText.CapLength(botsel->column - topsel->column); @@ -740,7 +752,9 @@ void UIMultiLineEdit::CopySelection(void) } line = LineFromLineNumber(botsel->line, true); - line.CapLength(botsel->column); + if (line.length() > botsel->column) { + line.CapLength(botsel->column); + } clipText += "\n" + line; }