--- lbbs/src/editor.c 2025/06/13 11:20:24 1.18 +++ lbbs/src/editor.c 2025/06/14 11:15:46 1.21 @@ -126,6 +126,15 @@ EDITOR_DATA *editor_data_load(const char memcpy(p_editor_data->p_display_lines[i], p_data + line_offsets[i], (size_t)p_editor_data->display_line_lengths[i]); current_data_line_length += p_editor_data->display_line_lengths[i]; + + // Trim \n from last line + if (i + 1 == p_editor_data->display_line_total && + p_editor_data->display_line_lengths[i] > 0 && + p_editor_data->p_display_lines[i][p_editor_data->display_line_lengths[i] - 1] == '\n') + { + p_editor_data->display_line_lengths[i]--; + current_data_line_length--; + } p_data_line[current_data_line_length] = '\0'; } @@ -218,6 +227,14 @@ int editor_data_insert(EDITOR_DATA *p_ed return -1; } + // Validate str + if ((str_len == 1 && str[0] <= 0) || + (str_len == 2 && (str[0] >= 0 || str[1] >= 0))) + { + log_error("Invalid input str, len=%d\n", str_len); + return -2; + } + // Get accurate offset of first character of CJK at offset position for (i = 0; i < offset; i++) { @@ -400,6 +417,13 @@ int editor_data_insert(EDITOR_DATA *p_ed *p_offset -= p_editor_data->display_line_lengths[*p_display_line]; (*p_display_line)++; } + else if (*p_display_line + 1 >= MAX_EDITOR_DATA_LINES) + { + len = split_line(p_editor_data->p_display_lines[*p_display_line], SCREEN_COLS - 1, &eol, &display_len); + p_editor_data->p_display_lines[*p_display_line][len] = '\0'; + p_editor_data->display_line_lengths[*p_display_line] = len; + *p_offset = len; + } } return 0; @@ -662,15 +686,16 @@ int editor_display(EDITOR_DATA *p_editor input_str[str_len] = (char)(ch - 256); str_len++; } - else + else if (str_len > 0) { + log_error("Received %d character over 127 followed by character less than 127\n", str_len); str_len = 0; } if ((ch >= 32 && ch < 127) || (ch > 127 && ch <= 255 && str_len == 2) || // Printable character or GBK ch == CR || ch == KEY_ESC) // Special character { - if (str_len == 0) + if (str_len == 0) // ch >= 32 && ch < 127 { input_str[0] = (char)ch; str_len = 1; @@ -695,12 +720,9 @@ int editor_display(EDITOR_DATA *p_editor input_str, str_len, &last_updated_line) < 0) { log_error("editor_data_insert(str_len=%d) error\n", str_len); - str_len = 0; } else { - str_len = 0; - output_end_row = MIN(SCREEN_ROWS - 1, output_current_row + (int)(last_updated_line - line_current)); line_current -= (output_current_row - row_pos); output_current_row = (int)row_pos; @@ -731,6 +753,7 @@ int editor_display(EDITOR_DATA *p_editor col_pos = offset_out + 1; } + str_len = 0; continue; } else if (ch == KEY_DEL || ch == BACKSPACE) // Del @@ -798,6 +821,7 @@ int editor_display(EDITOR_DATA *p_editor clrline(output_current_row, output_end_row); } + str_len = 0; continue; }