LibLine: Fix Shell crashing (due to write() EFAULT) on <tab><tab>

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.
This commit is contained in:
Andreas Kling 2020-04-01 11:24:10 +02:00
parent 9ba9bba529
commit 296f87fa7f
Notes: sideshowbarker 2024-07-19 08:01:17 +09:00

View file

@ -25,10 +25,10 @@
*/
#include "Editor.h"
#include <AK/StringBuilder.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <unistd.h>
namespace Line {
@ -323,16 +323,14 @@ String Editor::get_line(const String& prompt)
num_printed += fprintf(stderr, "%-*s", static_cast<int>(longest_suggestion_length) + 2, suggestion.characters());
}
putchar('\n');
struct iovec iov[] = {
{ const_cast<char*>(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();
}