Do not try to copy zero-length buffers in editor_type_newline()

While under Sortix these operations ought to be safe, UBSan will
complain regardless if you do a zero-byte memmove from NULL. This
caused the editor forcibly quit whenever it tried to open a file
that had an empty first line and at least one another line.
This commit is contained in:
Juhani Krekelä 2021-04-27 20:52:04 +00:00 committed by Jonas 'Sortie' Termansen
parent 287425ac5a
commit 86fa692c74
1 changed files with 6 additions and 2 deletions

View File

@ -63,12 +63,16 @@ void editor_type_newline(struct editor* editor)
keep_line->data = (wchar_t*) malloc(sizeof(wchar_t) * keep_length);
keep_line->used = keep_length;
keep_line->length = keep_length;
memcpy(keep_line->data, old_line.data + 0, sizeof(wchar_t) * keep_length);
if ( keep_length )
memcpy(keep_line->data, old_line.data + 0,
sizeof(wchar_t) * keep_length);
move_line->data = (wchar_t*) malloc(sizeof(wchar_t) * move_length);
move_line->used = move_length;
move_line->length = move_length;
memcpy(move_line->data, old_line.data + keep_length, sizeof(wchar_t) * move_length);
if ( move_length )
memcpy(move_line->data, old_line.data + keep_length,
sizeof(wchar_t) * move_length);
editor_cursor_set(editor, editor->cursor_row+1, 0);