diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index 077cd12..6d60145 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -648,9 +648,11 @@ String Terminal::_copy_screen(ScreenCopyFunction func) { char *out; PackedByteArray data; - data.resize(func(screen, &out)); - memcpy(data.ptrw(), out, data.size()); - std::free(out); + data.resize(std::max(func(screen, &out), 0)); + if (data.size() > 0) { + memcpy(data.ptrw(), out, data.size()); + std::free(out); + } return data.get_string_from_utf8(); } diff --git a/test/test_terminal.gd b/test/test_terminal.gd index 8775db7..b47b188 100644 --- a/test/test_terminal.gd +++ b/test/test_terminal.gd @@ -222,6 +222,9 @@ class TestCopy: subject.write(text) assert_string_contains(subject.copy_all(), text) + func test_copy_selection_when_nothing_selected(): + assert_eq(subject.copy_selection(), "") + class TestClear: extends TerminalTest