diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-03-30 00:37:30 +0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-30 11:29:52 +0200 |
commit | 77601e09c8f1c3c9a6cad5e4881262e6fafb6a92 (patch) | |
tree | 61cf6e435637b9db3128b6ab47819e69c3f7cab2 /Userland | |
parent | aff774c8ac6ca05a1f19b01833febedd8762f98b (diff) | |
download | serenity-77601e09c8f1c3c9a6cad5e4881262e6fafb6a92.zip |
FontEditor+TextEditor+Playground: Refuse to load device files
This prevents the undefined behaviour that would come up as a result of
doing so. (For example: opening "infinite" devices like /dev/full will
result in an infinite loop until exhaustion of memory)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Applications/FontEditor/main.cpp | 16 | ||||
-rw-r--r-- | Userland/Applications/TextEditor/TextEditorWidget.cpp | 5 | ||||
-rw-r--r-- | Userland/DevTools/Playground/main.cpp | 9 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/BitmapFont.cpp | 3 |
4 files changed, 31 insertions, 2 deletions
diff --git a/Userland/Applications/FontEditor/main.cpp b/Userland/Applications/FontEditor/main.cpp index 7055b49708..db91ce1c51 100644 --- a/Userland/Applications/FontEditor/main.cpp +++ b/Userland/Applications/FontEditor/main.cpp @@ -80,7 +80,13 @@ int main(int argc, char** argv) path = "/tmp/saved.font"; edited_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::FontDatabase::default_font().clone()); } else { - edited_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::BitmapFont::load_from_file(path)->clone()); + auto bitmap_font = Gfx::BitmapFont::load_from_file(path); + if (!bitmap_font) { + String message = String::formatted("Couldn't load font: {}\n", path); + GUI::MessageBox::show(nullptr, message, "Font Editor", GUI::MessageBox::Type::Error); + return 1; + } + edited_font = static_ptr_cast<Gfx::BitmapFont>(bitmap_font->clone()); if (!edited_font) { String message = String::formatted("Couldn't load font: {}\n", path); GUI::MessageBox::show(nullptr, message, "Font Editor", GUI::MessageBox::Type::Error); @@ -112,7 +118,13 @@ int main(int argc, char** argv) if (!open_path.has_value()) return; - RefPtr<Gfx::BitmapFont> new_font = static_ptr_cast<Gfx::BitmapFont>(Gfx::BitmapFont::load_from_file(open_path.value())->clone()); + auto bitmap_font = Gfx::BitmapFont::load_from_file(open_path.value()); + if (!bitmap_font) { + String message = String::formatted("Couldn't load font: {}\n", open_path.value()); + GUI::MessageBox::show(window, message, "Font Editor", GUI::MessageBox::Type::Error); + return; + } + RefPtr<Gfx::BitmapFont> new_font = static_ptr_cast<Gfx::BitmapFont>(bitmap_font->clone()); if (!new_font) { String message = String::formatted("Couldn't load font: {}\n", open_path.value()); GUI::MessageBox::show(window, message, "Font Editor", GUI::MessageBox::Type::Error); diff --git a/Userland/Applications/TextEditor/TextEditorWidget.cpp b/Userland/Applications/TextEditor/TextEditorWidget.cpp index 071076e9f0..09a946562f 100644 --- a/Userland/Applications/TextEditor/TextEditorWidget.cpp +++ b/Userland/Applications/TextEditor/TextEditorWidget.cpp @@ -639,6 +639,11 @@ bool TextEditorWidget::open_file(const String& path) return false; } + if (file->is_device()) { + GUI::MessageBox::show(window(), String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error); + return false; + } + m_editor->set_text(file->read_all()); m_document_dirty = false; m_document_opening = true; diff --git a/Userland/DevTools/Playground/main.cpp b/Userland/DevTools/Playground/main.cpp index a542caa53f..592fd31526 100644 --- a/Userland/DevTools/Playground/main.cpp +++ b/Userland/DevTools/Playground/main.cpp @@ -139,6 +139,10 @@ int main(int argc, char** argv) GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", path, strerror(errno)), "Error", GUI::MessageBox::Type::Error); return 1; } + if (file->is_device()) { + GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: Can't open device files", path), "Error", GUI::MessageBox::Type::Error); + return 1; + } editor.set_text(file->read_all()); } @@ -164,6 +168,11 @@ int main(int argc, char** argv) return; } + if (file->is_device()) { + GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: Can't open device files", open_path.value()), "Error", GUI::MessageBox::Type::Error); + return; + } + editor.set_text(file->read_all()); editor.set_focus(true); })); diff --git a/Userland/Libraries/LibGfx/BitmapFont.cpp b/Userland/Libraries/LibGfx/BitmapFont.cpp index b26db96c15..376525064b 100644 --- a/Userland/Libraries/LibGfx/BitmapFont.cpp +++ b/Userland/Libraries/LibGfx/BitmapFont.cpp @@ -172,6 +172,9 @@ size_t BitmapFont::glyph_count_by_type(FontTypes type) RefPtr<BitmapFont> BitmapFont::load_from_file(const StringView& path) { + if (Core::File::is_device(path)) + return nullptr; + auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; |