Skip to content

Commit

Permalink
LibLine: Fix writing to temporary file in ^X^E handler
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alimpfard authored and linusg committed Apr 30, 2021
1 parent f91bfe8 commit 6783f65
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Userland/Libraries/LibLine/InternalFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/FileStream.h>
#include <AK/ScopeGuard.h>
#include <AK/ScopedValueRollback.h>
#include <AK/StringBuilder.h>
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6783f65

Please sign in to comment.