-
Notifications
You must be signed in to change notification settings - Fork 100
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
Logging macros aren't safe in all contexts #113
Comments
We can certainly make the macros not robust but calling a macro like this (without the curly braces) is simply "wrong" / very bad practice. The logging macro could even be compiled out for performance in which case the calling code really needs to use curly braces (if you don't want to replace the macro with a no-op). |
Ah, good point, I had forgotten about the fact that the macros could be compiled out. I ended up opening a PR on the original problem code to add the braces: ros-drivers/joystick_drivers#126 . I still think it is a good idea to make the macros more robust, so I'll leave this open (though it is low priority). |
If you think it is an improvement (I completely agree) I would suggest to implement the suggested change now than adding it to the (already huge) backlog (under the assumption that it is a fairly small change anddoesn't take much time). |
It will require a semi-colon after each macro call, doesn't it ? (not saying it shouldn't be done just that it may be more work than just updating the macro definitions) |
Why would it? It depends on what the macro expands to. Since optionally it could expand to nothing I don't think it the macro "call" should be followed by a semicolon since that would be left standing on its own in that case. |
It's more subtle than that, unfortunately. The two-line change I just made was to add
The other way to do it is to remove the semi-colon from
But what's nice about requiring the semicolon at the call site is that it is always syntactically valid, even without curly-braces. That is, given the following code:
If
On the other hand, if If
which is still syntactically valid. |
Yeah that's what I meant by when saying it will require a semi-colon. 👍 On the other hand our styleguide and linters are meant to prevent the "no curly-braces" situation from happening. |
A semicolon after the macro only works when you can make assumptions about how the macro is being implemented. Otherwise you end up with an "extra" semicolon - which is fine in some cases (e.g. after a |
I disagree, I think that assumption/coupling should be encouraged. If a macro looks like a function call then it should behave like one by requiring a |
Just a brief list of references I found ad hoc of our current practice not to place a semicolon after a macro:
Imo we chose this for a reason and don't think we should change this now for this specific ticket. The feature described in this ticket should be implemented following our current practice. We could certainly open a discussion about the topic in a separate ticket (I don't think we should but I encourage everyone to do that and describe the pros and cons to convince the team that a change should happen). But it should prevent moving forward on this item since the decision is pretty much orthogonal (if a change is made the whole code base would need to be updated accordingly). |
Unfortunately, it is not completely orthogonal. The problem is that we have two choices on how to update the macro:
|
After we discussed this, the general consensus was to go with option 2 in #113 (comment); that is, add semicolons everywhere. I'm going to use this issue as the meta-issue to track all of the PRs to make this happen. Here is the list:
And here is CI for the whole lot: |
All looks good to me, pending CI. |
Just to be sure, I checked out all packages that are currently released into Bouncy to see which ones would be affected by this change. Outside of the PRs above, no additional Bouncy packages would need changes. This doesn't mean other, unreleased packages won't need to be changed, but the number should be small enough that we can deal with it as they come in. With that, I've merged all of the PRs for this, so I'll close this out. |
Consider the following structure:
With the current ROS2 rcutils, this code will fail to compile with something like:
I have a minimal reproducer of this problem over at https://github.com/clalancette/badmacro . We probably need to wrap all of these macros in the
do { ... } while(0)
construct to make them safe in these circumstances.The text was updated successfully, but these errors were encountered: