-
Notifications
You must be signed in to change notification settings - Fork 474
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
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. |
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. |
@iv461 can you share more details about the issue? |
Hey @alsora, sure, here is what I did: I've implemented an approach that works in isolation as you can see in this godbolt example: This example triggers a deprecation warning with GCC 13.4 — the same compiler I use to build rclcpp. However, if I put this exact code inside rclcpp, as I did in this commit: df1e782, the warning does not trigger at all. I've ruled out it being a build/setup issue: if I compile the same minimal Godbolt code as a ROS node, the warning triggers. I've also ruled out the possibility that I'm pulling the rclcpp headers from /opt/rclcpp by mistake – this is not the case. |
Pulls: #2954 |
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.