Fix -Wmaybe-uninitialized warning#2081
Conversation
206a603 to
e2028c4
Compare
rclcpp/src/rclcpp/context.cpp
Outdated
| std::unordered_set<std::shared_ptr<ShutdownCallbackHandle::ShutdownCallbackType>> * | ||
| callback_list_ptr = nullptr; |
There was a problem hiding this comment.
While I agree that this is a bug, I don't think that this is the right way to fix this. In particular, if the shutdown_type below ends up not being one of the cases, then we'll just go ahead and dereference the nullptr. I guess that is better in some ways than derferencing random memory, but it is not correct.
I think the proper thing to do here is probably to add a default label to the switch statement below, and probably throw an exception if it is not one of the expected shutdown types.
The same logic goes for below.
There was a problem hiding this comment.
If only called with values that are handled by the switch, this is not a bug. But I agree that a more defensive approach is in order. The way the code is now, introducing actual bugs later is easier than it should be. TBH, I only wanted to silence the warning. I'm glad that you're in favor of a better solution.
The enum in the switch statements is only used internally from within context.cpp, and when calling {add,remove,get}_shutdown_callback(), it is always a constant. So this is actually a compile-time parameter. Adding a default label with a throw, i.e., detecting this at runtime, I would not find appropriate. Instead, I made this a compile-time parameter now, so we can static_assert that it has one of the supported values.
Let me know what you think!
gcc 12 warned about `callback_list_ptr` potentially being uninitialized when compiling in release mode (i.e., with `-O2`). Since `shutdown_type` is a compile-time parameter, we fix the warning by enforcing the decision at compile time. Signed-off-by: Alexander Hans <ahans@users.noreply.github.com>
e2028c4 to
70e222e
Compare
rclcpp/src/rclcpp/context.cpp
Outdated
| const auto callback_shared_ptr = callback_handle.callback.lock(); | ||
| if (callback_shared_ptr == nullptr) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
I'm not sure if this even needs to be protected by the mutex. I did not pull it out to retain the original behavior. But if it doesn't need to be protected, moving it up would be better.
There was a problem hiding this comment.
yeah, right. callback_handle does not need to be protected by mutex in this function. probably move the mutex down here to protect the race condition for callback_set is better.
There was a problem hiding this comment.
Yeah, we can move it to only protect callback_set. I doubt it makes much of a difference in practice, though.
| const ShutdownCallbackHandle & callback_handle); | ||
|
|
||
| template<ShutdownType shutdown_type> | ||
| RCLCPP_LOCAL |
There was a problem hiding this comment.
Not having the RCLCPP_LOCAL here seemed like an oversight.
clalancette
left a comment
There was a problem hiding this comment.
Overall, I like the approach here; it takes care of the original issue, and it should be a bit faster (since we aren't doing the switch statement at runtime).
That said, I'm confused by a C++ aspect of this. That is, how can you have template function declaration in the hpp file and the definition in the cpp file? In my understanding, templates generate code, so anyone who wants to call it necessarily needs the code in the header file (not the implementation). How does this work?
You need the template definition when you instantiate a template. In the present case, the only instantiations are the calls from the three public functions in |
|
There's also the pattern of explicit template instantiation that allows you to reduce compile times and improve encapsulation: When you know upfront the types a template will be instantiated with, you can put the template definition in the However, in the present case none of that matters, since the methods are private. Having the definitions in the header would yield the exact same code, but compilation of thing that |
fujitatomoya
left a comment
There was a problem hiding this comment.
lgtm with green CI
| const ShutdownCallbackHandle & callback_handle); | ||
|
|
||
| template<ShutdownType shutdown_type> | ||
| RCLCPP_LOCAL |
rclcpp/src/rclcpp/context.cpp
Outdated
| const auto callback_shared_ptr = callback_handle.callback.lock(); | ||
| if (callback_shared_ptr == nullptr) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
yeah, right. callback_handle does not need to be protected by mutex in this function. probably move the mutex down here to protect the race condition for callback_set is better.
Yep, that makes sense. I figured it was something like that, but I just wanted to check. Thanks for the explanation. |
clalancette
left a comment
There was a problem hiding this comment.
This looks good to me, except for the one change below. Once we move the mutex in the remove_callback in remove_shutdown_callback, I'll approve and run CI for it.
Signed-off-by: Alexander Hans <ahans@users.noreply.github.com>
e0f172d to
e929dc3
Compare
Alright, pulled that block out of the lambda now! |
clalancette
left a comment
There was a problem hiding this comment.
This looks good to me. I'm going to fire up CI on it, but I'd appreciate another look from @fujitatomoya before I merge.
* Fix -Wmaybe-uninitialized warning gcc 12 warned about `callback_list_ptr` potentially being uninitialized when compiling in release mode (i.e., with `-O2`). Since `shutdown_type` is a compile-time parameter, we fix the warning by enforcing the decision at compile time. Signed-off-by: Alexander Hans <ahans@users.noreply.github.com>
* Fix -Wmaybe-uninitialized warning gcc 12 warned about `callback_list_ptr` potentially being uninitialized when compiling in release mode (i.e., with `-O2`). Since `shutdown_type` is a compile-time parameter, we fix the warning by enforcing the decision at compile time. Signed-off-by: Alexander Hans <ahans@users.noreply.github.com>
* Fix -Wmaybe-uninitialized warning gcc 12 warned about `callback_list_ptr` potentially being uninitialized when compiling in release mode (i.e., with `-O2`). Since `shutdown_type` is a compile-time parameter, we fix the warning by enforcing the decision at compile time. Signed-off-by: Alexander Hans <ahans@users.noreply.github.com>
* Fix -Wmaybe-uninitialized warning gcc 12 warned about `callback_list_ptr` potentially being uninitialized when compiling in release mode (i.e., with `-O2`). Since `shutdown_type` is a compile-time parameter, we fix the warning by enforcing the decision at compile time. Signed-off-by: Alexander Hans <ahans@users.noreply.github.com>
gcc 12 warns about this when compiling in release mode (i.e., with
-O2):