Fix #133414: Sequencer: Ensure valid UTF-8 sequence when pasting text
`TextVars::text` is only 512 bytes and does not include string ending byte, and `sequencer_text_edit_paste_exec` uses a very simple truncation that didn't guarantee valid UTF-8 sequence at the end. This is now fixed by using `BLI_str_utf8_invalid_strip` after truncating the string. Pull Request: https://projects.blender.org/blender/blender/pulls/133416
This commit is contained in:
@@ -820,16 +820,23 @@ static int sequencer_text_edit_paste_exec(bContext *C, wmOperator * /*op*/)
|
||||
}
|
||||
|
||||
const int max_str_len = sizeof(data->text) - (BLI_strnlen(data->text, sizeof(data->text)) + 1);
|
||||
clipboard_len = std::min(clipboard_len, max_str_len);
|
||||
|
||||
/* Maximum bytes that can be filled into `data->text`. */
|
||||
const int fillable_len = std::min(clipboard_len, max_str_len);
|
||||
|
||||
/* Truncated string could contain invalid utf-8 sequence, thus ensure the length inserted is
|
||||
* always valid. */
|
||||
size_t valid_str_len;
|
||||
const int extra_offset = BLI_strnlen_utf8_ex(clipboard_buf, fillable_len, &valid_str_len);
|
||||
|
||||
const seq::CharInfo cur_char = character_at_cursor_offset_get(text, data->cursor_offset);
|
||||
char *cursor_addr = const_cast<char *>(cur_char.str_ptr);
|
||||
const size_t move_str_len = BLI_strnlen(cursor_addr, sizeof(data->text)) + 1;
|
||||
|
||||
std::memmove(cursor_addr + clipboard_len, cursor_addr, move_str_len);
|
||||
std::memcpy(cursor_addr, clipboard_buf, clipboard_len);
|
||||
std::memmove(cursor_addr + valid_str_len, cursor_addr, move_str_len);
|
||||
std::memcpy(cursor_addr, clipboard_buf, valid_str_len);
|
||||
|
||||
data->cursor_offset += BLI_strlen_utf8(clipboard_buf);
|
||||
data->cursor_offset += extra_offset;
|
||||
|
||||
MEM_freeN(clipboard_buf);
|
||||
text_editing_update(C);
|
||||
|
||||
Reference in New Issue
Block a user