From e0be8a3f598d43e7ce6df6ff2a1eb5d7ac817177 Mon Sep 17 00:00:00 2001 From: FalseHonesty Date: Thu, 15 Apr 2021 12:42:40 -0400 Subject: HackStudio: Don't crash when invalid file is requested to be opened Previously, if the running debugger asked for HackStudio to open an invalid file, it would crash trying to switch to it. Now it will just continue without switching the editor. --- Userland/DevTools/HackStudio/HackStudioWidget.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'Userland/DevTools/HackStudio/HackStudioWidget.cpp') diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index b8a6deac45..3382a5dfe2 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -223,15 +223,15 @@ Vector HackStudioWidget::selected_file_paths() const return files; } -void HackStudioWidget::open_file(const String& full_filename) +bool HackStudioWidget::open_file(const String& full_filename) { String filename = full_filename; if (full_filename.starts_with(project().root_path())) { filename = LexicalPath::relative_path(full_filename, project().root_path()); } dbgln("HackStudio is opening {}", filename); - if (Core::File::is_directory(filename)) - return; + if (Core::File::is_directory(filename) || !Core::File::exists(filename)) + return false; if (!currently_open_file().is_empty()) { // Since the file is previously open, it should always be in m_open_files. @@ -282,6 +282,7 @@ void HackStudioWidget::open_file(const String& full_filename) current_editor_wrapper().filename_label().set_text(filename); current_editor().set_focus(true); + return true; } EditorWrapper& HackStudioWidget::current_editor_wrapper() @@ -647,7 +648,8 @@ void HackStudioWidget::initialize_debugger() make( [this, source_position, ®s](auto&) { m_current_editor_in_execution = get_editor_of_file(source_position.value().file_path); - m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); + if (m_current_editor_in_execution) + m_current_editor_in_execution->editor().set_execution_position(source_position.value().line_number - 1); m_debug_info_widget->update_state(*Debugger::the().session(), regs); m_debug_info_widget->set_debug_actions_enabled(true); m_disassembly_widget->update_state(*Debugger::the().session(), regs); @@ -689,7 +691,7 @@ String HackStudioWidget::get_full_path_of_serenity_source(const String& file) return String::formatted("{}/{}", serenity_sources_base, relative_path_builder.to_string()); } -NonnullRefPtr HackStudioWidget::get_editor_of_file(const String& file_name) +RefPtr HackStudioWidget::get_editor_of_file(const String& file_name) { String file_path = file_name; @@ -700,7 +702,8 @@ NonnullRefPtr HackStudioWidget::get_editor_of_file(const String& file_path = get_full_path_of_serenity_source(file_name); } - open_file(file_path); + if (!open_file(file_path)) + return nullptr; return current_editor_wrapper(); } -- cgit v1.2.3