-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix gcc [-Wsign-compare] warning #4192
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
@fmanno Can you please sign https://developer.mbed.org/contributor_agreement/ ? |
/morph test |
I've signed the agreement at https://developer.mbed.org/contributor_agreement/ and linked my mbed profile to my github profile. Cheers! |
/morph test |
@studavekar @bridadan I think CI is not being triggered... |
/morph test |
Result: FAILUREYour command has finished executing! Here's what you wrote!
OutputExample Build failed! |
platform/mbed_board.c
Outdated
if (size > 0) { | ||
if (!stdio_uart_inited) { | ||
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX); | ||
} | ||
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES | ||
for (unsigned int i = 0; i < size; i++) { | ||
for (char stdio_out_prev = '\0', int i = 0; i < size; i++) { |
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.
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.
Ah, yes. I've missed that second definition... reverting to having stdio_out_prev
defined outside of the loop should be ok.
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.
You're right, this should've been right before the loop not inside it.
I've pushed a new commit to fix the declaration of stdio_out_prev. This time to test the change I've forced MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES to be 1 to trigger the compile error first and later to check that the new code compiles. |
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 for the changes!
/morph test |
Result: FAILUREYour command has finished executing! Here's what you wrote!
|
Can you please rebase this branch to remove this merge commit? |
[Warning] mbed_board.c@99,36: comparison between signed and unsigned integer expressions [-Wsign-compare] is seen during compilation. Fix the warning and small improvements. 1. Change type of loop variable "i" to the same type as "size". Size is > 0 checked by the if statement and i stops at == size so none can be negative. However size still needs to be signed to detect error codes from vsnprintf. 2. Reduced scope of stdio_out_prev and make sure it's initialized. 3. Define a name for the error buffer size and use vsnprintf instead of vsprintf to avoid writing outside of the array. NOTE: the if(size > 0) statement doesn't need to change. If the message to write is larger than the buffer size vsnprintf truncates it automatically but still returns a positive value.
3fba122
to
4398b1f
Compare
/morph test |
Result: SUCCESSYour command has finished executing! Here's what you wrote!
OutputAll builds and test passed! |
Description
[Warning] mbed_board.c@99,36: comparison between signed and unsigned
integer expressions [-Wsign-compare] is seen during compilation.
Fix the warning and small improvements.
Size is > 0 checked by the if statement and the for loop stops at i == size
so none can be negative. However size still needs to be signed
to detect error codes from vsnprintf.
of vsprintf to avoid writing outside of the array.
NOTE: the
if(size > 0)
statement doesn't need to change. Ifthe message to write is larger than the buffer size vsnprintf
truncates it automatically but still returns a positive value.
Status
READY
Steps to test or reproduce