From 63030ae1d468f0cb096d36ab4f2dd71b0184a772 Mon Sep 17 00:00:00 2001 From: Ryan Friedman Date: Fri, 9 Jun 2023 19:15:15 -0600 Subject: [PATCH] add-Wnull-dereference and fix warnings Signed-off-by: Ryan Friedman --- nav2_common/cmake/nav2_package.cmake | 2 +- .../nav2_costmap_2d/denoise/image_processing.hpp | 5 ++++- nav2_mppi_controller/test/critic_manager_test.cpp | 14 ++++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/nav2_common/cmake/nav2_package.cmake b/nav2_common/cmake/nav2_package.cmake index c7046c837e4..6cce57440cd 100644 --- a/nav2_common/cmake/nav2_package.cmake +++ b/nav2_common/cmake/nav2_package.cmake @@ -37,7 +37,7 @@ macro(nav2_package) endif() if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") - add_compile_options(-Wall -Wextra -Wpedantic -Werror -Wdeprecated -fPIC ) + add_compile_options(-Wall -Wextra -Wpedantic -Werror -Wdeprecated -fPIC -Wnull-dereference) add_compile_options("$<$:-Wnon-virtual-dtor>") endif() diff --git a/nav2_costmap_2d/include/nav2_costmap_2d/denoise/image_processing.hpp b/nav2_costmap_2d/include/nav2_costmap_2d/denoise/image_processing.hpp index ee798948475..4527e07fdd3 100644 --- a/nav2_costmap_2d/include/nav2_costmap_2d/denoise/image_processing.hpp +++ b/nav2_costmap_2d/include/nav2_costmap_2d/denoise/image_processing.hpp @@ -999,7 +999,10 @@ class GroupsRemover // The group of pixels labeled 0 corresponds to empty map cells. // Zero bin of the histogram is equal to the number of pixels in this group. // Because the values of empty map cells should not be changed, we will reset this bin - groups_sizes.front() = 0; // don't change image background value + if (!groups_sizes.empty()) { + groups_sizes.front() = 0; // don't change image background value + } + // noise_labels_table[i] = true if group with label i is noise std::vector noise_labels_table(groups_sizes.size()); diff --git a/nav2_mppi_controller/test/critic_manager_test.cpp b/nav2_mppi_controller/test/critic_manager_test.cpp index 5a931de92fb..f24d205bfbe 100644 --- a/nav2_mppi_controller/test/critic_manager_test.cpp +++ b/nav2_mppi_controller/test/critic_manager_test.cpp @@ -65,12 +65,22 @@ class CriticManagerWrapper : public CriticManager bool getDummyCriticInitialized() { - return dynamic_cast(critics_[0].get())->initialized_; + const auto critic = critics_[0].get(); + if (critic == nullptr) { + return false; + } + const auto dummy_critic = dynamic_cast(critic); + return dummy_critic == nullptr ? false : dummy_critic->initialized_; } bool getDummyCriticScored() { - return dynamic_cast(critics_[0].get())->scored_; + const auto critic = critics_[0].get(); + if (critic == nullptr) { + return false; + } + const auto dummy_critic = dynamic_cast(critic); + return dummy_critic == nullptr ? false : dummy_critic->scored_; } };