-
Notifications
You must be signed in to change notification settings - Fork 58
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
dtls_debug.h: explicitly cast macro parameter to size_t. #217
Conversation
As far as I understand the test results, the Windows toolchain does not support |
85ab125
to
837d0e1
Compare
I would refrain from casting to |
I'm not sure, if I get it. The macros are all dumping an array of bytes with a certain length.
so, if we want similar behavior, we need to add that cast to the specific versions for zephyr and riot. Or did I miss something? |
I've checked this again, and I still don't see the point to "refrain from that cast to size_t". What this PR is fixing is the assumption in the LOG_DBG/LOG_DEBUG, where the |
I'm inclined to agree with obgm, based on the law of unintended consequences. As I read the code, |
Using macros in Zephyr (and RIOT?) allows to remove the logging code of some logging levels completely, so that they don't consume storage.
The code is
so I see the "unintended consequences" in not casting |
The problem is that int length = some_custom_function();
const char *data = some_other_custom_function;
dtls_debug_hexdump("foo", data, length); This happens all the time. Really. |
Sorry, I may be blind and/or stupid. What makes the difference of
In the second case, we may get a warning, or the printed result is somehow random. |
As I wrote above, this is expanded in a call to
which implicitly casts the |
An unintended consequence is when an unexpected negative |
Therefore the parameter In order to test the "protection" of not casting, I added
to dtls.c/dtls_handle_message and run a build on unix/posix (ubuntu 22.04).
with crash at runtime. if -1 is dangerous, it's hard to see the protection to not cast the parameter which is passed to "%zu". |
I'm guessing the crash is in the hexdump logic, so the invokers of |
Sure.
Exactly. And the same prerequisite about the value of length applies for the macro with the cast to If it's then considered, that the hexdump should be limited, e.g. to 1k, then this is an additional issue and with that we may adapt the macro again. |
In my example,
Yes but this is not a problem, we just need to do that. But for every cast we have to answer this question: "Is it correct to cast this type to |
Which type does "%zu" support? Isn't it "size_t"? |
Correct: |
Then this discussion seems to be based on what ever, but not the code change contained in this PR. |
Ok - unintended consequence.
which then translates (using your fix)
and so LOG_DEBUG is passed a large number rather than 4 (it is possible the resultant sum may overflow and wrap). Another unintended consequence
which then translates (using your fix)
No idea here as to what is typed to Passed parameters should be wrapped with ( ) as in
I agree that typecasting (if done properly) will get rid of your warnings generated by some compilers. |
Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
837d0e1
to
8267a24
Compare
#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, (size_t)(length)); LOG_HEXDUMP_DBG(buf, length, name); } | ||
#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%zu bytes):", name, (size_t)(length)); LOG_HEXDUMP_DBG(buf, length, name); } |
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.
name should be wrapped as (name) in 2 places per line, as well as buf and length wrapped in LOG_HEXDUMP()
#define dtls_debug_hexdump(name, buf, length) { LOG_DBG("%s (%zu bytes):", (name), (size_t)(length)); LOG_HEXDUMP_DBG((buf), (length), (name)); }
#define dtls_debug_dump(name, buf, length) { LOG_DBG("%s (%zu bytes):", (name), (size_t)(length)); LOG_HEXDUMP_DBG((buf), (length), (name)); }
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.
If we want to start with applying general rules about macros, that will change much more ;-).
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.
Agreed. However, every time we have to change a (#define) line of code, lets correctly fix that line and only correct the other lines that need doing when they need to be updated, so minimizing this particular fix.
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 disagree.
The tiny scope if this PR is based on an user request (see #212).
Extending the scope to "general/common rules", which are neither agreed nor discussed before is usually the way the generate quite a lot frustration, because that makes it impossible to improve the project in small steps.
If general rules about the design of macros should be applied to this project, I consider a separate issue for discussion will help much more than to "piggyback" this to tiny PRs.
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.
OK - what works best for you.
#define dtls_debug_hexdump(name, buf, length) { if (LOG_DEBUG <= LOG_LEVEL) { LOG_DEBUG("-= %s (%zu bytes) =-\n", name, (size_t)(length)); od_hex_dump(buf, length, 0); }} | ||
#define dtls_debug_dump(name, buf, length) { if (LOG_DEBUG <= LOG_LEVEL) { LOG_DEBUG("%s (%zu bytes):", name, (size_t)(length)); od_hex_dump(buf, length, 0); }} |
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.
Same as previous comment - things need to be wrapped in #define.
Currently upstream in discussion eclipse/tinydtls#217
Currently upstream in discussion eclipse/tinydtls#217
Currently upstream in discussion eclipse/tinydtls#217
Apart from my general concerns documented in #237 I wonder why this cast is necessary here, anyway: |
You can pass what ever C can convert into a "size_t". Even a uint8_t will work. |
This PR contains 1 change copied to 4 locations. It is pending now for 3 months. |
I'm happy for this to be merged as is. |
So, then just merge it. I still do not want it but since it is just for debug purposes and most IoT applications usually will not have to deal with numbers that exceed the number space of |
I'm sure, if that causes more trouble than it solves, we will simply get new issues. |
No description provided.