Skip to content

Commit 4398b1f

Browse files
committed
Fix gcc [-Wsign-compare] warning
[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.
1 parent 247238d commit 4398b1f

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

platform/mbed_board.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,16 @@ void mbed_error_printf(const char* format, ...) {
7575

7676
void mbed_error_vfprintf(const char * format, va_list arg) {
7777
#if DEVICE_SERIAL
78-
79-
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
80-
char stdio_out_prev;
81-
#endif
82-
78+
#define ERROR_BUF_SIZE (128)
8379
core_util_critical_section_enter();
84-
char buffer[128];
85-
int size = vsprintf(buffer, format, arg);
80+
char buffer[ERROR_BUF_SIZE];
81+
int size = vsnprintf(buffer, ERROR_BUF_SIZE, format, arg);
8682
if (size > 0) {
8783
if (!stdio_uart_inited) {
8884
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
8985
}
9086
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
87+
char stdio_out_prev = '\0';
9188
for (int i = 0; i < size; i++) {
9289
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
9390
serial_putc(&stdio_uart, '\r');

0 commit comments

Comments
 (0)