Skip to content

Commit

Permalink
Fix crash when redrawing after connecting external monitor
Browse files Browse the repository at this point in the history
When Kakoune's terminal is shown my laptop monitor and I plug in my
external monitor, the terminal's workspace will move to that external
monitor. When this happens, Kakoune segfaults consistently.

The crash happens in "TerminalUI::Screen::output" where we access a
dangling reference

	    auto output_line = [&](const Line& line) {
	        ColumnCount pending_move = 0;
	        for (auto& [text, skip, face] : line.atoms)
	        {
	/->          if (text.empty() and skip == 0)
	|                continue;
	|
	 cannot access memory referenced by "text"

The stack trace is

	Program received signal SIGSEGV, Segmentation fault.
	0x0000555555a5f1ac in Kakoune::String::Data::is_long (this=0x1a) at /home/johannes/git/kakoune/src/string.hh:180
	(gdb) bt
	#0  0x0000555555a5f1ac in Kakoune::String::Data::is_long (this=0x1a) at /home/johannes/git/kakoune/src/string.hh:180
	mawww#1  0x0000555555a5f1d6 in Kakoune::String::Data::size (this=0x1a) at /home/johannes/git/kakoune/src/string.hh:181
	mawww#2  0x0000555555de34e4 in Kakoune::String::length (this=0x1a) at /home/johannes/git/kakoune/src/string.hh:139
	mawww#3  Kakoune::StringOps<Kakoune::String, char>::empty (this=0x1a) at /home/johannes/git/kakoune/src/string.hh:68
	mawww#4  operator() (__closure=0x7fffffffba50, line=...) at terminal_ui.cc:303
	mawww#5  0x0000555555de45aa in Kakoune::TerminalUI::Screen::output (this=0x5555560a7a60, force=true, synchronized=true, writer=...) at terminal_ui.cc:371
	mawww#6  0x0000555555de548a in Kakoune::TerminalUI::redraw (this=0x5555560a7a40, force=true) at terminal_ui.cc:535
	mawww#7  0x0000555555de579e in Kakoune::TerminalUI::refresh (this=0x5555560a7a40, force=true) at terminal_ui.cc:556
	mawww#8  0x0000555555a9ae71 in Kakoune::Client::redraw_ifn (this=0x55555609f9b0) at client.cc:284
	mawww#9  0x0000555555aab3db in Kakoune::ClientManager::redraw_clients (this=0x7fffffffd060) at client_manager.cc:233
	mawww#10 0x0000555555c86ef8 in Kakoune::run_server (session="", server_init="", client_init="", init_buffer="fish-rust/src/ast.rs", init_coord=..., flags=Kakoune::ServerFlags::None, ui_type=Kakoune::UIType::Terminal,
	    debug_flags=Kakoune::DebugFlags::None, files=ArrayView<Kakoune::StringView> = {...}) at main.cc:908
	mawww#11 0x0000555555c8bb76 in main (argc=2, argv=0x7fffffffe7b8) at main.cc:1263

We check for terminal resize in

	TerminalUI::TerminalUI
	TerminalUI::get_next_key
	TerminalUI::draw
	TerminalUI::suspend

I think neither of those happens when the window is moved.

Looks like TerminalUI::refresh relies on up-to-date dimensions,
so add a resize check here too, fixing the crash.
  • Loading branch information
krobelus committed Mar 12, 2023
1 parent 38077ca commit 3546762
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/terminal_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,10 @@ void TerminalUI::set_cursor(CursorMode mode, DisplayCoord coord)
void TerminalUI::refresh(bool force)
{
if (m_dirty or force)
{
check_resize();
redraw(force);
}
m_dirty = false;
}

Expand Down

0 comments on commit 3546762

Please sign in to comment.