mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 17:15:08 +00:00
SQLStudio: Protect against possible crash when saving an empty file
The underlying Core::Stream methods require the bytes passed in to be non-empty. Simply opening the file is enough to ensure the file is created with empty contents if the editor's text is empty.
This commit is contained in:
parent
9b1e754d56
commit
b8e4ca3b0f
1 changed files with 14 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, Dylan Katz <dykatz@uw.edu>
|
* Copyright (c) 2022, Dylan Katz <dykatz@uw.edu>
|
||||||
|
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -37,15 +38,22 @@ ErrorOr<void> ScriptEditor::open_script_from_file(LexicalPath const& file_path)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ErrorOr<void> save_text_to_file(StringView filename, DeprecatedString text)
|
||||||
|
{
|
||||||
|
auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Write));
|
||||||
|
|
||||||
|
if (!text.is_empty())
|
||||||
|
TRY(file->write_entire_buffer(text.bytes()));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
ErrorOr<bool> ScriptEditor::save()
|
ErrorOr<bool> ScriptEditor::save()
|
||||||
{
|
{
|
||||||
if (m_path.is_empty())
|
if (m_path.is_empty())
|
||||||
return save_as();
|
return save_as();
|
||||||
|
|
||||||
auto file = TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Write));
|
TRY(save_text_to_file(m_path, text()));
|
||||||
auto editor_text = text();
|
|
||||||
TRY(file->write_entire_buffer(editor_text.bytes()));
|
|
||||||
|
|
||||||
document().set_unmodified();
|
document().set_unmodified();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -55,12 +63,9 @@ ErrorOr<bool> ScriptEditor::save_as()
|
||||||
auto maybe_save_path = GUI::FilePicker::get_save_filepath(window(), name(), "sql");
|
auto maybe_save_path = GUI::FilePicker::get_save_filepath(window(), name(), "sql");
|
||||||
if (!maybe_save_path.has_value())
|
if (!maybe_save_path.has_value())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto save_path = maybe_save_path.release_value();
|
auto save_path = maybe_save_path.release_value();
|
||||||
|
TRY(save_text_to_file(save_path, text()));
|
||||||
auto file = TRY(Core::Stream::File::open(save_path, Core::Stream::OpenMode::Write));
|
|
||||||
auto editor_text = text();
|
|
||||||
TRY(file->write_entire_buffer(editor_text.bytes()));
|
|
||||||
|
|
||||||
m_path = save_path;
|
m_path = save_path;
|
||||||
|
|
||||||
auto lexical_path = LexicalPath(save_path);
|
auto lexical_path = LexicalPath(save_path);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue