From 296f87fa7fb0c4c7e8a85143b9bc6273722902fc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 1 Apr 2020 11:24:10 +0200 Subject: [PATCH] LibLine: Fix Shell crashing (due to write() EFAULT) on Use a StringBuilder instead of blindly passing a bunch of potentially empty/null strings to the kernel. StringBuilder is more lenient and generally more pleasant to use anyway. --- Libraries/LibLine/Editor.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp index 9a2ff2c70e0..e5363b148f3 100644 --- a/Libraries/LibLine/Editor.cpp +++ b/Libraries/LibLine/Editor.cpp @@ -25,10 +25,10 @@ */ #include "Editor.h" +#include #include #include #include -#include #include namespace Line { @@ -323,16 +323,14 @@ String Editor::get_line(const String& prompt) num_printed += fprintf(stderr, "%-*s", static_cast(longest_suggestion_length) + 2, suggestion.characters()); } - putchar('\n'); - struct iovec iov[] = { - { const_cast(prompt.characters()), prompt.length() }, - { m_buffer.data(), m_cursor }, - { m_buffer.data() + m_cursor, m_buffer.size() - m_cursor } - }; - if (writev(STDOUT_FILENO, iov, 3)) { - perror("writev"); - ASSERT_NOT_REACHED(); - } + StringBuilder builder; + builder.append('\n'); + builder.append(prompt); + builder.append(m_buffer.data(), m_cursor); + builder.append(m_buffer.data() + m_cursor, m_buffer.size() - m_cursor); + fputs(builder.to_string().characters(), stdout); + fflush(stdout); + m_cursor = m_buffer.size(); }