-
Notifications
You must be signed in to change notification settings - Fork 471
Added static assert asserting custom types have no overloaded operator new #2954
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
base: rolling
Are you sure you want to change the base?
Conversation
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.
although I think this is a right thing to do to avoid undefined behavior by new/delete mismatch when the data object is copied by global allocator, this possibly breaks the application not to be able to compile once this fix is merged. again, i think this is also good to let the user application know the correct approach to publish the data in this particular case. but i would like to have other opinions from other developers.
@Mergifyio rebase |
✅ Branch has been successfully rebased |
54c60b2
to
74c26e3
Compare
Yes, this is undefined behavior, more speficially it might cause heap corruption and with this random crashes. But you are right, the problem is that there might be many places that need to be fixed, i.e. many packages that would fail to compile. To fix the compilation, generally it suffices to replace Two ideas:
By the way, I looked at the CI and it failed again due to an apparently unrelated issue in rclcpp_action. |
rclcpp/include/rclcpp/publisher.hpp
Outdated
"When publishing by value (i.e. when calling publish(const T& msg)), the published message" | ||
"type must not have an overloaded operator new. In this case, please use the" |
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.
Minor suggestion. Please double-check the line length.
"When publishing by value (i.e. when calling publish(const T& msg)), the published message" | |
"type must not have an overloaded operator new. In this case, please use the" | |
"When publishing by value (i.e. when calling publish(const T& msg)), the published message " | |
"type must not have an overloaded operator new. In this case, please use the " |
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've used now ament_uncrustify --reformat --linelength 100
for code formatting, which formatter do you use ?
We discussed this during the client library working group. If possible it would be better to make it a static warning rather than an assertion, to have a deprecation cycle. |
…r new Signed-off-by: Ivo Ivanov <[email protected]>
Signed-off-by: Ivo Ivanov <[email protected]>
This reverts commit f1917a35a5037220be2f537031e3ef5b65b5be3e. Signed-off-by: Ivo Ivanov <[email protected]>
Signed-off-by: Ivo Ivanov <[email protected]>
…nstead of an static_assert that would cause a compile error. Signed-off-by: Ivo Ivanov <[email protected]>
…late specialization. This forces the compiler to evaluate the condition later, not as early as in the lexial stage Signed-off-by: Ivo Ivanov <[email protected]>
…struct does not trigger Signed-off-by: Ivo Ivanov <[email protected]>
Signed-off-by: Ivo Ivanov <[email protected]>
…ept GCC: We need two patterns to avoid struct redeclaration error Signed-off-by: Ivo Ivanov <[email protected]>
Signed-off-by: Ivo Ivanov <[email protected]>
…g: The warning simply does not trigger when called from the rclcpp publish method for some reason Signed-off-by: Ivo Ivanov <[email protected]>
Signed-off-by: Ivo Ivanov <[email protected]>
Thanks for the update. I would also be fine with a deprecation warning instead of a compile error. However, I was unable to to implement a deprecation warning. I tried an approach in df1e782 which generally works, but for some strange reason I can't get it to work inside rclcpp. Therefore, I'll leave it as it is, with the static_assert. |
Description
Fixes #2951
Is this user-facing behavior change?
When users publish custom message types using type adapters, they must now publish by unique_ptr if the custom message type overloads operator new. This is to prevent new/delete mismatch.
Did you use Generative AI?
No
Additional Information
Tested, and correctly fails to compile if I try to publish a pcl::PointCloud by value AND I'm pulling the dependency on PCL via pcl_ros.
According to cppreference, there are 22 overloads of operator new. I only implement detecting the class-specific ones 15 and 17. I do not implement the array-operators (16 and 18) since the detection becomes ambiguous and causes compile error.
I do not think it is reasonable to guard against overloaded global operator new/delete, i.e. overloads 1-10.
Also, overloads 11-14 and 19-22 accept arbitrary arguments and are therefore likely to be undetectable.