-
Notifications
You must be signed in to change notification settings - Fork 102
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
Add parenthesis around the argument in time conversion macros defined in time.h #261
Add parenthesis around the argument in time conversion macros defined in time.h #261
Conversation
…efined in time.h ... to protect against unexpected results of calls with arguments that are a sum or with other operators, that have a lower precedence than the multiplication or division operators. Signed-off-by: Johannes Meyer <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change looks good to me.
While a test in rclcpp for this is OK, I'd prefer to have the test here in this package. That way the package that is providing the functionality is also testing it. Can you add a test for the problematic cases here?
Signed-off-by: Johannes Meyer <[email protected]>
Done in ab897e4. The new unit test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice set of tests, thanks! Will run CI next.
…t case Signed-off-by: Johannes Meyer <[email protected]>
test/test_time.cpp
Outdated
|
||
// seconds to nanoseconds | ||
EXPECT_EQ(static_cast<int64_t>(RCUTILS_S_TO_NS(1)), 1000000000ll); // int | ||
EXPECT_EQ(static_cast<int64_t>(RCUTILS_S_TO_NS(0.2)), 200000000ll); // double |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this (and all of the other double checks), it seems to me that instead of casting the result of the macro to int64_t, we should actually change the expected value to be a double. After all, that would more closely test that the output of the macro returns the type we expect, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is clearly beyond scope of this pull request, I think. Why would you expect the macros to return seconds as double
? They don't. They return the same type as their argument, and eventually promote the integer to the width of a long long
because of the ll
suffix of the constants.
But RCUTILS_MS_TO_S(1)
for example returns 0
, even if you would cast the result to double
afterwards. Calling the macros with a uint64_t
triggers a sign-conversion warning.
If you expect certain fixed input and output types, then they should probably be replaced by inline functions or the macros need some additional explicit casts. But that would be a potentially breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry, I associated your comment with the wrong line of code. Clearly, if the argument is of type double
, the output can be expected of type double
, too, and the cast to int64_t
can be omitted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry, I associated your comment with the wrong line of code. Clearly, if the argument is of type
double
, the output can be expected of typedouble
, too, and the cast toint64_t
can be omitted.
Yeah, that's what I was getting at. Sorry if I wasn't clear. Anyway, could you please make that change? Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thought was that the macro might be used to assign to an int64_t
, like
double seconds = 42.5;
int64_t nanoseconds = RCUTILS_S_TO_NS(seconds);
But I could not find any usage of these macros with a floating point argument and this code snippet triggers a -Wconversion
compiler warning, so probably they were just never meant for that purpose.
Signed-off-by: Johannes Meyer <[email protected]>
Are the test failures on Windows related to this patch? I do not see how. I am afraid that I cannot follow-up on this anymore. Creating and amending the unit tests for such a small and trivial patch as adding parentheses for macro arguments already cost too much time, and the multi-platform support makes it hard to test locally before applying changes (or I am not aware of the procedures yet). |
Yeah, I don't see how either. We have some known issues with services in ROS 2, so I'm going to chalk it up to that. Thanks for iterating, I'm going to go ahead and merge. |
... to protect against unexpected results of calls with sum arguments, or with other operators that have a lower precedence than the multiplication or division operators.
Related to ros2/rclcpp#1188: