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)
This commit is contained in:
parent
aff774c8ac
commit
77601e09c8
Notes:
sideshowbarker
2024-07-18 20:57:32 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/77601e09c8f Pull-request: https://github.com/SerenityOS/serenity/pull/6019 Issue: https://github.com/SerenityOS/serenity/issues/5874
4 changed files with 31 additions and 2 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}));
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue