From c52f28efa40773d926816ca7cdba8c8fea9cb825 Mon Sep 17 00:00:00 2001 From: WillyJL <49810075+Willy-JL@users.noreply.github.com> Date: Mon, 25 Mar 2024 06:28:20 +0000 Subject: [PATCH] GUI: Fix textbox overflow/crash/hang with 256+ lines (#3536) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * GUI: Fix textbox overflow/crash/hang with 256+ lines * GUI: Textbox calculate lines based on font height * Gui: proper types in text_box Co-authored-by: あく --- applications/services/gui/modules/text_box.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/applications/services/gui/modules/text_box.c b/applications/services/gui/modules/text_box.c index f80129eb9..9c2959b30 100644 --- a/applications/services/gui/modules/text_box.c +++ b/applications/services/gui/modules/text_box.c @@ -100,16 +100,19 @@ static void text_box_insert_endline(Canvas* canvas, TextBoxModel* model) { line_num++; model->text = furi_string_get_cstr(model->text_formatted); model->text_pos = (char*)model->text; - if(model->focus == TextBoxFocusEnd && line_num > 5) { + size_t lines_on_screen = 56 / canvas_current_font_height(canvas); + if(model->focus == TextBoxFocusEnd && line_num > lines_on_screen) { // Set text position to 5th line from the end - for(uint8_t i = 0; i < line_num - 5; i++) { - while(*model->text_pos++ != '\n') { - }; + const char* end = model->text + furi_string_size(model->text_formatted); + for(size_t i = 0; i < line_num - lines_on_screen; i++) { + while(model->text_pos < end) { + if(*model->text_pos++ == '\n') break; + } } - model->scroll_num = line_num - 4; - model->scroll_pos = line_num - 5; + model->scroll_num = line_num - (lines_on_screen - 1); + model->scroll_pos = line_num - lines_on_screen; } else { - model->scroll_num = MAX(line_num - 4, 0u); + model->scroll_num = MAX(line_num - (lines_on_screen - 1), 0u); model->scroll_pos = 0; } }