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

Fixing conditional jump based on uninitialized optional values #48

Merged
merged 2 commits into from
Nov 9, 2022

Conversation

bartek-kc
Copy link
Contributor

This pull request aims to fix a potentially dangerous usage of std::optional in which a conditional jump is made based on uninitialized value (or values). The fix eliminates the following error message issued by Valgrind: "Conditional jump or move depends on uninitialised value(s)". Quoting cppreference.com: std::optional<T>::operator* does not check whether the optional contains a value!

@bartek-kc bartek-kc requested a review from a team as a code owner November 8, 2022 19:22
@@ -31,7 +31,7 @@ void ToolBaseNode::make_subscribe_unsubscribe_decisions()
{
if (auto source_info = try_discover_source()) {
// always relay same topic type and QoS profile as the first available source
if (*topic_type_ != source_info->first || *qos_profile_ != source_info->second || !pub_) {
if (!topic_type_ || !qos_profile_ || *topic_type_ != source_info->first || *qos_profile_ != source_info->second || !pub_) {
Copy link
Member

Choose a reason for hiding this comment

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

Or || operators in if statement doesn't guarantee order of operations for processor.
Processor can execute *topic_type_ != source_info->first || *qos_profile_ != source_info->second || !pub_ and than check !topic_type_ || !qos_profile_ It seems fix doesn't fully address the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your reply, @MichaelOrlov. Aren't logical OR as well as AND operators exceptions to the rule of unspecified order of evaluation in an expression? According to cppreference.com: Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument. Therefore, should any of our optionals here be uninitialized, the || operator will short-circuit and won't evaluate its following arguments.

Copy link
Member

Choose a reason for hiding this comment

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

@bartek-kc Never mind, it seems after C++ 11 things got better and sequence of the or operations for || respecting as well.

Copy link
Member

@MichaelOrlov MichaelOrlov left a comment

Choose a reason for hiding this comment

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

@bartek-kc Please address linters warnings and sign your PR.

At least uncrustify complain about

 +++ topic_tools/src/tool_base_node.cpp.uncrustify
@@ -34 +34,3 @@
-    if (!topic_type_ || !qos_profile_ || *topic_type_ != source_info->first || *qos_profile_ != source_info->second || !pub_) {
+    if (!topic_type_ || !qos_profile_ || *topic_type_ != source_info->first ||
+      *qos_profile_ != source_info->second || !pub_)
+    {

Signed-off-by: bartek-kc <bartosz.kozlowiec@robotec.ai>
Signed-off-by: bartek-kc <bartosz.kozlowiec@robotec.ai>
@bartek-kc bartek-kc force-pushed the std_optional_usage_fix branch from 166909b to 20c0191 Compare November 9, 2022 11:57
@bartek-kc bartek-kc requested review from MichaelOrlov and removed request for emersonknapp and christophebedard November 9, 2022 11:59
@MichaelOrlov
Copy link
Member

Gist: https://gist.githubusercontent.com/MichaelOrlov/e9fdae3db05615033ea0a6e76be88b88/raw/f236f5a9d60c0b35f8e5b75b5050d709b41df425/ros2.repos
BUILD args: --packages-above-and-dependencies topic_tools
TEST args: --packages-above topic_tools
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/11100

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

@MichaelOrlov
Copy link
Member

Warnings on Windows build are unrelated to the changes in this PR and was existing before.

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.

2 participants