-
Notifications
You must be signed in to change notification settings - Fork 7.3k
virtual.c: Fix compiler warnings related to interface name #87650
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
include/zephyr/net/virtual.h
Outdated
#else | ||
#define VIRTUAL_MAX_NAME_LEN 0 | ||
#endif | ||
/** @endcond */ |
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.
what is /** @endcond */
? Should it stay?
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.
It's termination for the /** @cond INTERNAL_HIDDEN */
above, it keeps part of the header excluded from doxygen processing.
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.
Thanks, added it back.
86db19c
to
6a473b7
Compare
Added |
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 commit log topic should be
net: virtual: ...
But before fully accepting this, I would like to know why this code is a problem.
include/zephyr/net/virtual.h
Outdated
|
||
#if defined(CONFIG_NET_L2_VIRTUAL) |
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 whole point of the original code was that we do not need to add this ifdef here.
What is the problem with the compiler in this case, why it is complaining about 0 size array?
Note that if CONFIG_NET_L2_VIRTUAL
is not set, this struct is never used anywhere so this sounds like a false positive.
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.
Agree, that it was probably a false positive.
Removing the 0-length array was my addition. Because, as far as I know zero-length arrays are not portable. If it is not the case, I will revert, but then this code ctx->name[ARRAY_SIZE(ctx->name) - 1] = '\0';
looks dangerous (I know it is only compiled in if ctx->name > 0
but still. In that case I will add a static assertion instead.
Both patches, make it clearer IMO, because no context knowledge is needed.
For example
strncpy(ctx->name, name, ARRAY_SIZE(ctx->name));
ctx->name[ARRAY_SIZE(ctx->name) - 1] = '\0';
Is clear without additional context.
In the old code. The reader hat to know that. In some circumstances CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN
was the length of the ctx->name
and in others this function would not get compiled it.
Adjusted the commit message.
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.
@jukkar I could use 1
instead of 0
. Would you prefer this?
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.
Perhaps we should do []
instead which is portable, see #81895
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 defined(CONFIG_NET_L2_VIRTUAL)
#define VIRTUAL_MAX_NAME_LEN CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN
#else
/* Leave empty so it creates a flexible array member at the end */
#define VIRTUAL_MAX_NAME_LEN
#endif
Flexible array members are not my favorite. But I could live with it. Note for example MISRA disallows it. I believe it is intended only as last element of structs that are intended for dynamic allocation.
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.
But here the flex array is only used to silence the compiler, we are not using the struct in this case anywhere.
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, will try.
6a473b7
to
e0a71d7
Compare
Fix compiler warning by adjusting the number of chars copied to the destination. Compiler does not like if the destination size of the `strncpy`-operation is the same as the number of characters written. Even though it is not a bug in this case. Only copying size-1 characters fixes the warning and exhibits the same behavior. Signed-off-by: Cla Mattia Galliard <[email protected]>
e0a71d7
to
c30fdbd
Compare
Found the true source of the warning and adjusted the code and the description of this PR. |
(Also ajusted commit message) |
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.
Thanks, this looks good now.
Thank you for the quick feedback 👍 |
Fix compiler warning by adjusting the number of chars copied to the
destination. Compiler does not like if the destination size of the
strncpy
-operation is the same as the number of characters written. Eventhough it is not a bug in this case. Only copying size-1 characters fixes
the warning and exhibits the same behavior.