Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly check lib pointer for null #95

Merged
merged 3 commits into from
Dec 2, 2020
Merged

Conversation

brawner
Copy link
Contributor

@brawner brawner commented Nov 19, 2020

This showed up as a false positive from clang static analysis. It's because it doesn't realize that gtest ASSERT_* macros will return. Clang does allow for analyzer_noreturn annotations to help it understand, but I believe that's only on functions. Clang does recognize standard assert statements though, so I thought that would be the simplest solution here.

Example

/home/brawner/workspace/ros2_master/src/ros2/rosidl_typesupport/rosidl_typesupport_c/test/test_message_type_support_dispatch.cpp:117:15: warning: Called C++ object pointer is null
  EXPECT_TRUE(lib->has_symbol("test_message_type_support"));
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/brawner/workspace/ros2_master/install/gtest_vendor/src/gtest_vendor/include/gtest/gtest.h:1969:23: note: expanded from macro 'EXPECT_TRUE'
  GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \
                      ^~~~~~~~~
/home/brawner/workspace/ros2_master/install/gtest_vendor/src/gtest_vendor/include/gtest/internal/gtest-internal.h:1325:34: note: expanded from macro 'GTEST_TEST_BOOLEAN_'
      ::testing::AssertionResult(expression)) \
                                 ^~~~~~~~~~

Signed-off-by: Stephen Brawner brawner@gmail.com

Signed-off-by: Stephen Brawner <brawner@gmail.com>
@brawner
Copy link
Contributor Author

brawner commented Nov 20, 2020

Force pushed the same change to rosidl_typesupport_cpp

@brawner
Copy link
Contributor Author

brawner commented Nov 20, 2020

Testing --packages-select rosidl_typesupport_c rosidl_typesupport_cpp

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Comment on lines 119 to 123

ASSERT_NE(lib, nullptr);
// c-style assert is used to avoid false positive of clang static analysis because it can't
// follow gtest asserts
assert(nullptr != lib);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe a somewhat simpler solution here is:

Suggested change
ASSERT_NE(lib, nullptr);
// c-style assert is used to avoid false positive of clang static analysis because it can't
// follow gtest asserts
assert(nullptr != lib);
ASSERT(nullptr != lib);

(I'm not against what you are doing here on principal, but I think it would be a little simpler to just stick with the GTest macros if possible).

Same goes for the change below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that scan-build is not following gtest macros to know that it's impossible for lib to be null. I kept the ASSERT_NE in line 120 you'll notice so the assert in 123 is actually impossible to fail and gtest will report the test failure in line 120. But scan-build does acknowledge c-style assert macros, so it will correctly identify that lib can't be null in line 124.

As far as I can find, you can annotate that functions won't return, but I couldn't find the same for macros.

Copy link
Contributor

@sloretz sloretz Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just out of curiosity, mind changing ASSERT_NE to ASSERT_TRUE(nullptr != lib) and seeing if the analyzer still complains? It seems like there might be some built in magic for it.

https://reviews.llvm.org/D27773
https://clang.llvm.org/doxygen/GTestChecker_8cpp_source.html

Also found:
google/googletest#734

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that was my suggestion as well :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that seems to work. Fixed

Signed-off-by: Stephen Brawner <brawner@gmail.com>
@brawner
Copy link
Contributor Author

brawner commented Dec 2, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Stephen Brawner <brawner@gmail.com>
@brawner
Copy link
Contributor Author

brawner commented Dec 2, 2020

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@sloretz sloretz dismissed clalancette’s stale review December 2, 2020 21:25

Feedback has been implemented

@brawner
Copy link
Contributor Author

brawner commented Dec 2, 2020

cmake warning on windows is due to an older cmake version in fastcdr. Unrelated to this PR.

@brawner brawner merged commit d8c8c32 into master Dec 2, 2020
@delete-merged-branch delete-merged-branch bot deleted the brawner/object-pointer-null branch December 2, 2020 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants