diff options
| author | Johannes Altmanninger <aclopte@gmail.com> | 2023-03-12 09:25:55 +0100 |
|---|---|---|
| committer | Johannes Altmanninger <aclopte@gmail.com> | 2023-04-24 18:31:05 +0200 |
| commit | cf94b310aa2ea343c619845b89c00d6cc5a10e5d (patch) | |
| tree | 8c61d7db838f6cde2a23fd2e709103470ca6bc52 /src | |
| parent | 566eb25d7ae917d4bb71557384a0c21eb70b2a10 (diff) | |
Fix crash after multiple terminal resizes
When Kakoune's terminal is shown on my laptop monitor and I plug
in my external monitor, the terminal's workspace will move to that
external monitor. When this happens, Kakoune may segfault.
There are multiple resize events (SIGWINCH) in quick succession;
it crashes because we handle SIGWINCH during rendering.
The problem happens during execution of "TerminalUI::Screen::output"
(frame #18). When we receive SIGWINCH while writing to stdout, write(2)
fails with EAGAIN, prompting us to handle pending events (See ae001a1f9
(Run EventManager whenever writing to a file descriptor would block,
2022-05-10)). We update the screen size in check_resize() here:
#4 Kakoune::TerminalUI::check_resize (force=<optimized out>) at terminal_ui.cc:683
#5 Kakoune::TerminalUI::get_next_key () at terminal_ui.cc:719
#6 operator() (__closure=0x555555984198) at terminal_ui.cc:484
#7 std::__invoke_impl<void, Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>&, Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode> (__f=...) at /usr/include/c++/12.2.1/bits/invoke.h:61
#8 std::__invoke_r<void, Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>&, Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode> (__fn=...) at /usr/include/c++/12.2.1/bits/invoke.h:111
#9 std::_Function_handler<void(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode), Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)> >::_M_invoke(const std::_Any_data &, Kakoune::FDWatcher &, Kakoune::FdEvents &&, Kakoune::EventMode &&) (__functor=..., __args#0=..., __args#1=<optimized out>, __args#2=<optimized out>) at /usr/include/c++/12.2.1/bits/std_function.h:290
#10 std::function<void (Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>::operator()(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode) const (__args#2=<optimized out>, __args#1=<optimized out>, __args#0=...) at /usr/include/c++/12.2.1/bits/std_function.h:591
#11 Kakoune::FDWatcher::run (mode=Kakoune::EventMode::Urgent, events=<optimized out>) at event_manager.cc:28
#12 Kakoune::EventManager::handle_next_events (mode=mode@entry=Kakoune::EventMode::Urgent, sigmask=sigmask@entry=0x0, block=<optimized out>, block@entry=false) at event_manager.cc:143
#13 Kakoune::write (fd=1, data=...) at file.cc:273
#14 Kakoune::BufferedWriter<4096>::flush () at string.hh:236
#15 Kakoune::BufferedWriter<4096>::write (data="t file.hh:145
#16 Kakoune::TerminalUI::Screen::set_face (face=..., writer=...) at terminal_ui.cc:255
#17 operator() (line=..., __closure=<synthetic pointer>) at terminal_ui.cc:326
#18 Kakoune::TerminalUI::Screen::output (force=force@entry=true, synchronized=<optimized out>, writer=...) at terminal_ui.cc:402
#19 Kakoune::TerminalUI::redraw (force=force@entry=true) at terminal_ui.cc:571
#20 Kakoune::TerminalUI::refresh (force=<optimized out>) at terminal_ui.cc:592
#21 Kakoune::Client::redraw_ifn () at client.cc:282
#22 Kakoune::ClientManager::redraw_clients () at client_manager.cc:232
#23 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=<optimized out>, files=ArrayView<Kakoune::StringView> = {...}) at main.cc:893
#24 main (argc=<optimized out>, argv=<optimized out>) at main.cc:1243
Thereafter, "TerminalUI::Screen::output" resumes and crashes due to
a buffer overflow in "lines" which has been resized.
Diffstat (limited to 'src')
| -rw-r--r-- | src/file.cc | 10 | ||||
| -rw-r--r-- | src/file.hh | 5 | ||||
| -rw-r--r-- | src/terminal_ui.cc | 2 |
3 files changed, 11 insertions, 6 deletions
diff --git a/src/file.cc b/src/file.cc index a2ad279b..58eb9284 100644 --- a/src/file.cc +++ b/src/file.cc @@ -252,13 +252,14 @@ bool regular_file_exists(StringView filename) (st.st_mode & S_IFMT) == S_IFREG; } +template<bool atomic> void write(int fd, StringView data) { const char* ptr = data.data(); ssize_t count = (int)data.length(); int flags = fcntl(fd, F_GETFL, 0); - if (EventManager::has_instance()) + if (not atomic and EventManager::has_instance()) fcntl(fd, F_SETFL, flags | O_NONBLOCK); auto restore_flags = on_scope_end([&] { fcntl(fd, F_SETFL, flags); }); @@ -269,12 +270,15 @@ void write(int fd, StringView data) ptr += written; count -= written; } - else if (errno == EAGAIN and EventManager::has_instance()) + else if (errno == EAGAIN and not atomic and EventManager::has_instance()) EventManager::instance().handle_next_events(EventMode::Urgent, nullptr, false); else throw file_access_error(format("fd: {}", fd), strerror(errno)); } } +template void write<true>(int fd, StringView data); +template void write<false>(int fd, StringView data); + void write_to_file(StringView filename, StringView data) { @@ -295,7 +299,7 @@ void write_buffer_to_fd(Buffer& buffer, int fd) eoldata = "\n"; - BufferedWriter writer{fd}; + BufferedWriter<false> writer{fd}; if (buffer.options()["BOM"].get<ByteOrderMark>() == ByteOrderMark::Utf8) writer.write("\xEF\xBB\xBF"); diff --git a/src/file.hh b/src/file.hh index 1d9391fb..65abd3c8 100644 --- a/src/file.hh +++ b/src/file.hh @@ -39,6 +39,7 @@ bool fd_readable(int fd); bool fd_writable(int fd); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); +template<bool force_blocking = false> void write(int fd, StringView data); void write_to_file(StringView filename, StringView data); @@ -121,7 +122,7 @@ CandidateList complete_filename(StringView prefix, const Regex& ignore_regex, CandidateList complete_command(StringView prefix, ByteCount cursor_pos = -1); -template<int buffer_size = 4096> +template<bool atomic, int buffer_size = 4096> struct BufferedWriter { BufferedWriter(int fd) @@ -149,7 +150,7 @@ struct BufferedWriter void flush() { - Kakoune::write(m_fd, {m_buffer, m_pos}); + Kakoune::write<atomic>(m_fd, {m_buffer, m_pos}); m_pos = 0; } diff --git a/src/terminal_ui.cc b/src/terminal_ui.cc index d411dd67..9685129a 100644 --- a/src/terminal_ui.cc +++ b/src/terminal_ui.cc @@ -208,7 +208,7 @@ void TerminalUI::Window::draw(DisplayCoord pos, lines[(int)pos.line].append({}, size.column - pos.column, default_face); } -struct Writer : BufferedWriter<> +struct Writer : BufferedWriter<true> { using Writer::BufferedWriter::BufferedWriter; ~Writer() noexcept(false) = default; |
