mavros_extras: Fix buggy check for lat/lon ignored #1805
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I stumbled upon what I think is a bug here, thanks to a
-Wparentheses
warning I got (clang 13, but I'm guessing this is on gcc too).The warning text explains:
which I don't think is what we want here. The if statement will evaluate to false in some cases where it shouldn't, such as when:
type_mask
is 2 = 0b0010IGNORE_LATITUDE
is 1 = 0b0001IGNORE_LONGITUDE
is 2 = 0b0010then the if statement evaluates to
0b0010 & (0b0001 | 0b0010) > 0
0b0010 & 0b0011 > 0
0b0010 & true
because > evaluates first0b0010 & 0b0001
0b0000
false
which is wrong!
We can fix this by adding parentheses:
or just by removing the
> 0
comparison and allowing the statement to implicitly convert to boolif (position_target.type_mask & (mavros_msgs::GlobalPositionTarget::IGNORE_LATITUDE | mavros_msgs::GlobalPositionTarget::IGNORE_LONGITUDE))