From 43303a51bfccf4fbf7e5a1eb912898c024ad3ba1 Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Sun, 28 Apr 2024 15:25:26 +1200 Subject: [PATCH] fix(term): copy_selection() when nothing selected Fixes the copy_selection() method so that it returns and empty string when called and nothing is selected. Previously printed an error. --- addons/godot_xterm/native/src/terminal.cpp | 8 +++++--- test/test_terminal.gd | 3 +++ 2 files changed, 8 insertions(+), 3 deletions(-) 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