1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:28:10 +00:00

LibGUI: Avoid access to Clipboard server, clipboard text is never empty

The clipboard cannot reasonably contain the empty string. The clipboard
can be empty (i.e. cleared), sure, but that this check was about whether
the clipboard contained the empty string.

This cannot easily happen for two reasons:
- TextEditor GUI elements disable their copy actions when the selection
  is empty.
- Clipboard::set_data, through which all text-copying operates,
  implicitly forbids empty strings, because Process::sys$anon_create
  forbids empty anonymous files.
- Even if it were sent (e.g. by creating a non-empty anonymous file and
  sending it manually to the Clipboard server), it would not be
  received, because decode(Decoder&, Core::AnonymousBuffer&) goes
  through mmap() with a size of 0, which also is forbidden by the
  Kernel.

In other words, if the clipboard is never the empty text, therefore
checking this condition is pointless, and we can save a roundtrip to the
Clipboard server.
This commit is contained in:
Ben Wiederhake 2021-11-20 15:20:23 +01:00 committed by Linus Groh
parent c80dcc4671
commit 06f140a025

View file

@ -83,7 +83,7 @@ void TextEditor::create_actions()
m_cut_action->set_enabled(false);
m_copy_action->set_enabled(false);
m_paste_action = CommonActions::make_paste_action([&](auto&) { paste(); }, this);
m_paste_action->set_enabled(is_editable() && Clipboard::the().mime_type().starts_with("text/") && !Clipboard::the().data().is_empty());
m_paste_action->set_enabled(is_editable() && Clipboard::the().mime_type().starts_with("text/"));
if (is_multi_line()) {
m_go_to_line_action = Action::create(
"Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/go-forward.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) {
@ -1882,7 +1882,7 @@ void TextEditor::cursor_did_change()
void TextEditor::clipboard_content_did_change(String const& mime_type)
{
m_paste_action->set_enabled(is_editable() && mime_type.starts_with("text/") && !Clipboard::the().data().is_empty());
m_paste_action->set_enabled(is_editable() && mime_type.starts_with("text/"));
}
void TextEditor::set_document(TextDocument& document)