Skip to content

samples: http_server: Fix warning about integer name #93468

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

decsny
Copy link
Member

@decsny decsny commented Jul 21, 2025

When building with CONFIG_DEBUG=y, there is a build warning about the name being truncated due to integer size is larger than MAX_NAME_LEN. So fix with modulo operator to be clear to the compiler what we expect.

@decsny decsny added the Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc. label Jul 21, 2025
@zephyrbot zephyrbot added area: Sockets Networking sockets area: HTTP HTTP client/server support area: Samples Samples area: Networking size: XS A PR changing only a single line of code labels Jul 21, 2025
@@ -324,7 +324,7 @@ int ws_echo_setup(int ws_socket, struct http_request_ctx *request_ctx, void *use
#define MAX_NAME_LEN sizeof("ws[xx]")
char name[MAX_NAME_LEN];

snprintk(name, sizeof(name), "ws[%d]", slot);
snprintk(name, sizeof(name), "ws[%d]", slot % (10 * MAX_NAME_LEN));
Copy link
Member

Choose a reason for hiding this comment

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

This does not look right. The idea is to print the slot value here. If the name variable is too small, let's increase the size of it instead, so something like
MAX_NAME_LEN sizeof("ws[xxxx]")

Copy link
Member Author

@decsny decsny Jul 22, 2025

Choose a reason for hiding this comment

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

yeah this is not right what I have here, but I think what you suggested also won't help, the issue appears to be that the compiler knows that the max integer value is 10 digits, so the max size would theoretically be sizeof("ws[xxxxxxxxxx]"), is that what you think it should be changed to ?

Copy link
Member Author

Choose a reason for hiding this comment

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

I pushed the same idea as you said but did sizeof("ws[]") + 10

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this sizeof("ws[xxxxxxxxxx]") is what I was suggesting, so just adding enough x to silence the warning.

Copy link
Member

Choose a reason for hiding this comment

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

Having +10 certainly works, but it is now a magical constant and not immediately obvious why it is there. Therefore we used the "template" way in other part of the code and I suggest we do that here too.

When building with CONFIG_DEBUG=y, there is a build warning about the
name being truncated due to integer size is larger than MAX_NAME_LEN.
So fix MAX_NAME_LEN to be large enough to handle 10 digit integer, which
is maximum.

Signed-off-by: Declan Snyder <[email protected]>
@decsny decsny force-pushed the fix/http_server_ws_name_warning branch from d81befc to 1e995c6 Compare July 22, 2025 21:39
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: HTTP HTTP client/server support area: Networking area: Samples Samples area: Sockets Networking sockets size: XS A PR changing only a single line of code Trivial Changes that can be reviewed by anyone, i.e. doc changes, minor build system tweaks, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants