From 86fa692c74686acc1efcb0102c62b0f18d5cf2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Tue, 27 Apr 2021 20:52:04 +0000 Subject: [PATCH] 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. --- editor/command.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/editor/command.c b/editor/command.c index 46a5c3f9..bf63151f 100644 --- a/editor/command.c +++ b/editor/command.c @@ -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);