GUI: Fix textbox overflow/crash/hang with 256+ lines (#3536)

* 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: あく <alleteam@gmail.com>
This commit is contained in:
WillyJL 2024-03-25 06:28:20 +00:00 committed by GitHub
parent 0a48658a9a
commit c52f28efa4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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;
}
}