-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
base: main
Are you sure you want to change the base?
samples: http_server: Fix warning about integer name #93468
Conversation
@@ -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)); |
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.
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]")
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.
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 ?
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 pushed the same idea as you said but did sizeof("ws[]") + 10
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.
Yes, this sizeof("ws[xxxxxxxxxx]")
is what I was suggesting, so just adding enough x to silence the warning.
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.
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]>
d81befc
to
1e995c6
Compare
|
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.