From 6783f65d5a013c181b9d89f2667d188eff86d4e8 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sat, 1 May 2021 00:17:54 +0430 Subject: [PATCH] LibLine: Fix writing to temporary file in ^X^E handler I have no idea how this _ever_ worked, and I also have no idea why past me didn't use FileStream to begin with. Fixes the issue where lots of junk data would be written to the temp file, causing the external editor to crash. --- Userland/Libraries/LibLine/InternalFunctions.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibLine/InternalFunctions.cpp b/Userland/Libraries/LibLine/InternalFunctions.cpp index 1eb3177b3f..8c0964078d 100644 --- a/Userland/Libraries/LibLine/InternalFunctions.cpp +++ b/Userland/Libraries/LibLine/InternalFunctions.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -527,15 +528,15 @@ void Editor::edit_in_external_editor() return; } + OutputFileStream stream { fp }; + StringBuilder builder; builder.append(Utf32View { m_buffer.data(), m_buffer.size() }); - auto view = builder.string_view(); - size_t remaining_size = view.length(); - - while (remaining_size > 0) - remaining_size = fwrite(view.characters_without_null_termination() - remaining_size, sizeof(char), remaining_size, fp); - - fclose(fp); + auto bytes = builder.string_view().bytes(); + while (!bytes.is_empty()) { + auto nwritten = stream.write(bytes); + bytes = bytes.slice(nwritten); + } } ScopeGuard remove_temp_file_guard {