Skip to content

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

Merged
merged 1 commit into from
Mar 27, 2025

Conversation

clamattia
Copy link
Collaborator

@clamattia clamattia commented Mar 25, 2025

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.

#else
#define VIRTUAL_MAX_NAME_LEN 0
#endif
/** @endcond */
Copy link
Collaborator Author

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?

Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added it back.

rlubos
rlubos previously approved these changes Mar 26, 2025
@clamattia
Copy link
Collaborator Author

Added doxygen endcond back

rlubos
rlubos previously approved these changes Mar 26, 2025
pdgendt
pdgendt previously approved these changes Mar 26, 2025
Copy link
Member

@jukkar jukkar left a 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.

Comment on lines 163 to 164

#if defined(CONFIG_NET_L2_VIRTUAL)
Copy link
Member

@jukkar jukkar Mar 26, 2025

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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?

Copy link
Member

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

Copy link
Collaborator Author

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.

Copy link
Member

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will try.

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]>
@clamattia clamattia dismissed stale reviews from pdgendt and rlubos via c30fdbd March 26, 2025 16:05
@clamattia
Copy link
Collaborator Author

Found the true source of the warning and adjusted the code and the description of this PR.
Please have another look @jukkar

@clamattia
Copy link
Collaborator Author

(Also ajusted commit message)

Copy link
Member

@jukkar jukkar left a 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.

@jukkar jukkar requested review from pdgendt and rlubos March 26, 2025 16:20
@clamattia
Copy link
Collaborator Author

Thanks, this looks good now.

Thank you for the quick feedback 👍

@kartben kartben merged commit 72fcca0 into zephyrproject-rtos:main Mar 27, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants