From 57f3f22aa4bd4ebd0936760a0771bb11dda7ed06 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sun, 3 Aug 2025 12:51:13 +0100 Subject: [PATCH] nullptr safety --- .../3d_navlib/nl_3d_viewer_plugin_impl.cpp | 20 +++++-------------- eeschema/navlib/nl_schematic_plugin_impl.cpp | 15 +++++++------- gerbview/navlib/nl_gerbview_plugin_impl.cpp | 6 +++--- .../navlib/nl_pl_editor_plugin_impl.cpp | 4 ++-- pcbnew/navlib/nl_pcbnew_plugin_impl.cpp | 15 +++++++------- 5 files changed, 26 insertions(+), 34 deletions(-) diff --git a/3d-viewer/3d_navlib/nl_3d_viewer_plugin_impl.cpp b/3d-viewer/3d_navlib/nl_3d_viewer_plugin_impl.cpp index 5cd3045f87..f2ec72bc54 100644 --- a/3d-viewer/3d_navlib/nl_3d_viewer_plugin_impl.cpp +++ b/3d-viewer/3d_navlib/nl_3d_viewer_plugin_impl.cpp @@ -544,9 +544,7 @@ long NL_3D_VIEWER_PLUGIN_IMPL::SetHitSelectionOnly( bool onlySelection ) long NL_3D_VIEWER_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) { if( commandId.empty() ) - { return 0; - } std::list actions = ACTION_MANAGER::GetActionList(); TOOL_ACTION* context = nullptr; @@ -557,9 +555,7 @@ long NL_3D_VIEWER_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) std::string nm = action->GetName(); if( commandId == nm ) - { context = action; - } } if( context != nullptr ) @@ -568,27 +564,21 @@ long NL_3D_VIEWER_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) // Only allow command execution if the window is enabled. i.e. there is not a modal dialog // currently active. - TOOLS_HOLDER* tools_holder = nullptr; - - if( parent->IsEnabled() && ( tools_holder = dynamic_cast( parent ) ) ) + if( parent && parent->IsEnabled() ) { - TOOL_MANAGER* tool_manager = tools_holder->GetToolManager(); + TOOLS_HOLDER* tools_holder = dynamic_cast( parent ); + TOOL_MANAGER* tool_manager = tools_holder ? tools_holder->GetToolManager() : nullptr; - if( tool_manager == nullptr ) - { + if( !tool_manager ) return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); - } // Get the selection to use to test if the action is enabled SELECTION& sel = tool_manager->GetToolHolder()->GetCurrentSelection(); bool runAction = true; - if( const ACTION_CONDITIONS* aCond = - tool_manager->GetActionManager()->GetCondition( *context ) ) - { + if( const ACTION_CONDITIONS* aCond = tool_manager->GetActionManager()->GetCondition( *context ) ) runAction = aCond->enableCondition( sel ); - } if( runAction ) { diff --git a/eeschema/navlib/nl_schematic_plugin_impl.cpp b/eeschema/navlib/nl_schematic_plugin_impl.cpp index dc6bdaa646..44eeb00805 100644 --- a/eeschema/navlib/nl_schematic_plugin_impl.cpp +++ b/eeschema/navlib/nl_schematic_plugin_impl.cpp @@ -432,21 +432,22 @@ long NL_SCHEMATIC_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) // Only allow command execution if the window is enabled. i.e. there is not a modal dialog // currently active. - - if( parent->IsEnabled() ) + if( parent && parent->IsEnabled() ) { - TOOL_MANAGER* tool_manager = static_cast( parent )->GetToolManager(); + TOOLS_HOLDER* tools_holder = dynamic_cast( parent ); + TOOL_MANAGER* tool_manager = tools_holder ? tools_holder->GetToolManager() : nullptr; + + // Only allow for command execution if the tool manager is accessible. + if( !tool_manager ) + return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); // Get the selection to use to test if the action is enabled SELECTION& sel = tool_manager->GetToolHolder()->GetCurrentSelection(); bool runAction = true; - if( const ACTION_CONDITIONS* aCond = - tool_manager->GetActionManager()->GetCondition( *context ) ) - { + if( const ACTION_CONDITIONS* aCond = tool_manager->GetActionManager()->GetCondition( *context ) ) runAction = aCond->enableCondition( sel ); - } if( runAction ) tool_manager->RunAction( *context ); diff --git a/gerbview/navlib/nl_gerbview_plugin_impl.cpp b/gerbview/navlib/nl_gerbview_plugin_impl.cpp index 946b828bca..4176189468 100644 --- a/gerbview/navlib/nl_gerbview_plugin_impl.cpp +++ b/gerbview/navlib/nl_gerbview_plugin_impl.cpp @@ -432,7 +432,8 @@ long NL_GERBVIEW_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) if( !parent || !parent->IsEnabled() ) return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); - TOOL_MANAGER* tool_manager = dynamic_cast( parent )->GetToolManager(); + TOOLS_HOLDER* tools_holder = dynamic_cast( parent ); + TOOL_MANAGER* tool_manager = tools_holder ? tools_holder->GetToolManager() : nullptr; // Only allow for command execution if the tool manager is accessible. if( !tool_manager ) @@ -448,8 +449,7 @@ long NL_GERBVIEW_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) // Get the selection to use to test if the action is enabled const SELECTION& sel = tool_manager->GetToolHolder()->GetCurrentSelection(); - const ACTION_CONDITIONS* aCond = - tool_manager->GetActionManager()->GetCondition( *action ); + const ACTION_CONDITIONS* aCond = tool_manager->GetActionManager()->GetCondition( *action ); if( !aCond ) return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); diff --git a/pagelayout_editor/navlib/nl_pl_editor_plugin_impl.cpp b/pagelayout_editor/navlib/nl_pl_editor_plugin_impl.cpp index 395f12c273..643c4a594d 100644 --- a/pagelayout_editor/navlib/nl_pl_editor_plugin_impl.cpp +++ b/pagelayout_editor/navlib/nl_pl_editor_plugin_impl.cpp @@ -436,14 +436,14 @@ long NL_PL_EDITOR_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) // Only allow command execution if the window is enabled. i.e. there is not a modal dialog // currently active. - if( parent == nullptr || !parent->IsEnabled() ) + if( !parent || !parent->IsEnabled() ) return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); TOOLS_HOLDER* tools_holder = dynamic_cast( parent ); TOOL_MANAGER* tool_manager = tools_holder ? tools_holder->GetToolManager() : nullptr; // Only allow for command execution if the tool manager is accessible. - if( tool_manager == nullptr ) + if( !tool_manager ) return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); for( std::list actions = ACTION_MANAGER::GetActionList(); const auto action : actions ) diff --git a/pcbnew/navlib/nl_pcbnew_plugin_impl.cpp b/pcbnew/navlib/nl_pcbnew_plugin_impl.cpp index b8f70a88cb..9648c0ccc3 100644 --- a/pcbnew/navlib/nl_pcbnew_plugin_impl.cpp +++ b/pcbnew/navlib/nl_pcbnew_plugin_impl.cpp @@ -417,21 +417,22 @@ long NL_PCBNEW_PLUGIN_IMPL::SetActiveCommand( std::string commandId ) // Only allow command execution if the window is enabled. i.e. there is not a modal dialog // currently active. - - if( parent->IsEnabled() ) + if( parent && parent->IsEnabled() ) { - TOOL_MANAGER* tool_manager = static_cast( parent )->GetToolManager(); + TOOLS_HOLDER* tools_holder = dynamic_cast( parent ); + TOOL_MANAGER* tool_manager = tools_holder ? tools_holder->GetToolManager() : nullptr; + + // Only allow for command execution if the tool manager is accessible. + if( !tool_manager ) + return navlib::make_result_code( navlib::navlib_errc::invalid_operation ); // Get the selection to use to test if the action is enabled SELECTION& sel = tool_manager->GetToolHolder()->GetCurrentSelection(); bool runAction = true; - if( const ACTION_CONDITIONS* aCond = - tool_manager->GetActionManager()->GetCondition( *context ) ) - { + if( const ACTION_CONDITIONS* aCond = tool_manager->GetActionManager()->GetCondition( *context ) ) runAction = aCond->enableCondition( sel ); - } if( runAction ) tool_manager->RunAction( *context );