From ac4fce50ce4b838f72aa3eea9b6506a49eaaa4d9 Mon Sep 17 00:00:00 2001 From: nakst <> Date: Tue, 4 Jan 2022 21:45:26 +0000 Subject: [PATCH] textbox bugfix; more assertions in textbox on caret position --- desktop/textbox.cpp | 29 ++++++++++++++++++++++++++--- util/x11/build.sh | 2 +- util/x11/platform.cpp | 2 ++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/desktop/textbox.cpp b/desktop/textbox.cpp index 5e2f45f..8eb9ce9 100644 --- a/desktop/textbox.cpp +++ b/desktop/textbox.cpp @@ -50,6 +50,7 @@ struct EsTextbox : EsElement { TextboxCaret carets[2]; // carets[1] is the actual caret; carets[0] is the selection anchor. TextboxCaret wordSelectionAnchor, wordSelectionAnchor2; + bool wordSelectionAnchorValid; Array lines; Array visibleLines; @@ -759,6 +760,11 @@ void EsTextboxMoveCaret(EsTextbox *textbox, int32_t line, int32_t byte) { textbox->carets[1].byte = byte; textbox->Repaint(true); TextboxUpdateCommands(textbox, true); + + EsAssert(textbox->carets[0].line >= 0 && textbox->carets[0].line < (int32_t) textbox->lines.Length() + && textbox->carets[0].byte >= 0 && textbox->carets[0].byte <= textbox->lines[textbox->carets[0].line].lengthBytes); + EsAssert(textbox->carets[1].line >= 0 && textbox->carets[1].line < (int32_t) textbox->lines.Length() + && textbox->carets[1].byte >= 0 && textbox->carets[1].byte <= textbox->lines[textbox->carets[1].line].lengthBytes); } void EsTextboxGetSelection(EsTextbox *textbox, int32_t *fromLine, int32_t *fromByte, int32_t *toLine, int32_t *toByte) { @@ -783,6 +789,11 @@ void EsTextboxSetSelection(EsTextbox *textbox, int32_t fromLine, int32_t fromByt textbox->Repaint(true); TextboxUpdateCommands(textbox, true); EsTextboxEnsureCaretVisible(textbox); + + EsAssert(textbox->carets[0].line >= 0 && textbox->carets[0].line < (int32_t) textbox->lines.Length() + && textbox->carets[0].byte >= 0 && textbox->carets[0].byte <= textbox->lines[textbox->carets[0].line].lengthBytes); + EsAssert(textbox->carets[1].line >= 0 && textbox->carets[1].line < (int32_t) textbox->lines.Length() + && textbox->carets[1].byte >= 0 && textbox->carets[1].byte <= textbox->lines[textbox->carets[1].line].lengthBytes); } void EsTextboxSelectAll(EsTextbox *textbox) { @@ -860,8 +871,7 @@ void EsTextboxInsert(EsTextbox *textbox, const char *string, ptrdiff_t stringByt TextboxUndoItemHeader *undoItem = nullptr; size_t undoItemBytes = 0; - textbox->wordSelectionAnchor = textbox->carets[0]; - textbox->wordSelectionAnchor2 = textbox->carets[1]; + textbox->wordSelectionAnchorValid = false; textbox->verticalMotionHorizontalDepth = -1; @@ -1179,6 +1189,11 @@ void EsTextboxInsert(EsTextbox *textbox, const char *string, ptrdiff_t stringByt textbox->scroll.Refresh(); TextboxUpdateCommands(textbox, true); + + EsAssert(textbox->carets[0].line >= 0 && textbox->carets[0].line < (int32_t) textbox->lines.Length() + && textbox->carets[0].byte >= 0 && textbox->carets[0].byte <= textbox->lines[textbox->carets[0].line].lengthBytes); + EsAssert(textbox->carets[1].line >= 0 && textbox->carets[1].line < (int32_t) textbox->lines.Length() + && textbox->carets[1].byte >= 0 && textbox->carets[1].byte <= textbox->lines[textbox->carets[1].line].lengthBytes); } char *EsTextboxGetContents(EsTextbox *textbox, size_t *_bytes, uint32_t flags) { @@ -1381,6 +1396,7 @@ bool TextboxFindCaret(EsTextbox *textbox, int positionX, int positionY, bool sec ptrdiff_t result = TextGetCharacterAtPoint(textbox, &textbox->textStyle, GET_BUFFER(line), line->lengthBytes, &pointX, ES_TEXT_GET_CHARACTER_AT_POINT_MIDDLE); + EsAssert(result >= -1 && result <= line->lengthBytes); textbox->carets[1].byte = result == -1 ? line->lengthBytes : result; } @@ -1397,13 +1413,15 @@ bool TextboxFindCaret(EsTextbox *textbox, int positionX, int positionY, bool sec TextboxMoveCaret(textbox, textbox->carets + 1, MOVE_CARET_FORWARDS, MOVE_CARET_WORD, true); textbox->wordSelectionAnchor = textbox->carets[0]; textbox->wordSelectionAnchor2 = textbox->carets[1]; + textbox->wordSelectionAnchorValid = true; } else if (clickChainCount == 3) { TextboxMoveCaret(textbox, textbox->carets + 0, MOVE_CARET_BACKWARDS, MOVE_CARET_LINE, true); TextboxMoveCaret(textbox, textbox->carets + 1, MOVE_CARET_FORWARDS, MOVE_CARET_LINE, true); textbox->wordSelectionAnchor = textbox->carets[0]; textbox->wordSelectionAnchor2 = textbox->carets[1]; + textbox->wordSelectionAnchorValid = true; } - } else { + } else if (textbox->wordSelectionAnchorValid) { if (clickChainCount == 2) { if (TextboxCompareCarets(textbox->carets + 1, textbox->carets + 0) < 0) { TextboxMoveCaret(textbox, textbox->carets + 1, MOVE_CARET_BACKWARDS, MOVE_CARET_WORD); @@ -1424,6 +1442,11 @@ bool TextboxFindCaret(EsTextbox *textbox, int positionX, int positionY, bool sec } } + EsAssert(textbox->carets[0].line >= 0 && textbox->carets[0].line < (int32_t) textbox->lines.Length() + && textbox->carets[0].byte >= 0 && textbox->carets[0].byte <= textbox->lines[textbox->carets[0].line].lengthBytes); + EsAssert(textbox->carets[1].line >= 0 && textbox->carets[1].line < (int32_t) textbox->lines.Length() + && textbox->carets[1].byte >= 0 && textbox->carets[1].byte <= textbox->lines[textbox->carets[1].line].lengthBytes); + TextboxUpdateCommands(textbox, true); return textbox->carets[0].line != startLine0 || textbox->carets[1].line != startLine1; } diff --git a/util/x11/build.sh b/util/x11/build.sh index f1a5b5f..9f6cf8e 100755 --- a/util/x11/build.sh +++ b/util/x11/build.sh @@ -4,4 +4,4 @@ mkdir -p bin/include_x11 cp root/Applications/POSIX/include/essence.h bin/include_x11 cp root/Essence/Desktop.esx bin/bundle.dat ld -r -b binary -o bin/Object\ Files/bundle.o bin/bundle.dat -g++ -o bin/hello_x11 util/x11/platform.cpp apps/text_editor.cpp bin/Object\ Files/bundle.o -lfreetype -lharfbuzz -lX11 -pthread -g -fno-exceptions -Ibin/include_x11 -I. -I/usr/include/freetype2 -DNO_API_TABLE -DUSE_PLATFORM_HEAP -DUSE_FREETYPE_AND_HARFBUZZ -DUSE_STB_SPRINTF -D_start=_StartApplication -D_init=EsHeapValidate -DES_FORWARD -Wall -Wextra -Wno-empty-body -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -fsanitize=address +g++ -o bin/hello_x11 util/x11/platform.cpp $1 bin/Object\ Files/bundle.o -lfreetype -lharfbuzz -lX11 -pthread -g -fno-exceptions -Ibin/include_x11 -I. -I/usr/include/freetype2 -DNO_API_TABLE -DUSE_PLATFORM_HEAP -DUSE_FREETYPE_AND_HARFBUZZ -DUSE_STB_SPRINTF -D_start=_StartApplication -D_init=EsHeapValidate -DES_FORWARD -Wall -Wextra -Wno-empty-body -Wno-deprecated-declarations -Wno-unknown-pragmas -Wno-missing-field-initializers -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -fsanitize=address diff --git a/util/x11/platform.cpp b/util/x11/platform.cpp index 5b4b040..3734424 100644 --- a/util/x11/platform.cpp +++ b/util/x11/platform.cpp @@ -499,6 +499,8 @@ uintptr_t _APISyscall(uintptr_t index, uintptr_t argument0, uintptr_t argument1, pthread_mutex_unlock(&windowsMutex); return ES_SUCCESS; + } else if (index == ES_SYSCALL_PROCESS_CRASH) { + assert(false); } else { fprintf(stderr, "Unimplemented system call '%s' (%ld).\n", EnumLookupNameFromValue(enumStrings_EsSyscallType, index), index); exit(1);