mirror of https://github.com/ARMmbed/mbed-os.git
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.pull/4192/head
parent
247238d993
commit
4398b1f3c1
|
@ -75,19 +75,16 @@ void mbed_error_printf(const char* format, ...) {
|
||||||
|
|
||||||
void mbed_error_vfprintf(const char * format, va_list arg) {
|
void mbed_error_vfprintf(const char * format, va_list arg) {
|
||||||
#if DEVICE_SERIAL
|
#if DEVICE_SERIAL
|
||||||
|
#define ERROR_BUF_SIZE (128)
|
||||||
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
|
|
||||||
char stdio_out_prev;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
core_util_critical_section_enter();
|
core_util_critical_section_enter();
|
||||||
char buffer[128];
|
char buffer[ERROR_BUF_SIZE];
|
||||||
int size = vsprintf(buffer, format, arg);
|
int size = vsnprintf(buffer, ERROR_BUF_SIZE, format, arg);
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
if (!stdio_uart_inited) {
|
if (!stdio_uart_inited) {
|
||||||
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
|
serial_init(&stdio_uart, STDIO_UART_TX, STDIO_UART_RX);
|
||||||
}
|
}
|
||||||
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
|
#if MBED_CONF_PLATFORM_STDIO_CONVERT_NEWLINES
|
||||||
|
char stdio_out_prev = '\0';
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
|
if (buffer[i] == '\n' && stdio_out_prev != '\r') {
|
||||||
serial_putc(&stdio_uart, '\r');
|
serial_putc(&stdio_uart, '\r');
|
||||||
|
|
Loading…
Reference in New Issue