Frente a una advertencia que no soy capaz de resolver. Estoy usando stm32 MCU y STM32CubeIDE con un compilador C11 estándar. Puedo deshacerme fácilmente de la advertencia aumentando el tamaño de la matriz gStr , pero me gustaría saber cuál es el problema aquí. El tamaño actual de gStr parece adecuado para contener los datos proporcionados. Cualquier ayuda es apreciada. ¡Gracias!
Las definiciones globales de las variables utilizadas se ven así:
#define LOG_ERROR_FILENAME_MAX_LEN 30 #define LOG_ERROR_MAX_ENTRY 30 #define LOG_GSTR_SIZE 128 //changed from 128 to 2048 to remove warning typedef struct { char fileName[LOG_ERROR_FILENAME_MAX_LEN]; u16 line; u16 hits; } LogEntryType; static LogEntryType gLogEntrys[LOG_ERROR_MAX_ENTRY]; u8 gLogEntryCount = 0; static char gStr[LOG_GSTR_SIZE];La llamada de función donde se genera la advertencia se ve así:
snprintf(gStr, LOG_GSTR_SIZE, "%s line:%u hits:%u",gLogEntrys[i].fileName, gLogEntrys[i].line, gLogEntrys[i].hits); La llamada snprintf está generando la siguiente advertencia
../User/Utils/log_error.c: In function 'cliCmdDisplayLog': ../User/Utils/log_error.c:54:36: **warning**: '%s' directive output may be truncated writing up to 1019 bytes into a region of size 128 [-Wformat-truncation=] 54 | snprintf(gStr, LOG_GSTR_SIZE, "%s line:%u hits:%u", | ^~ ../User/Utils/log_error.c:54:35: note: directive argument in the range [0, 65535] 54 | snprintf(gStr, LOG_GSTR_SIZE, "%s line:%u hits:%u", | ^~~~~~~~~~~~~~~~~~~~ ../User/Utils/log_error.c:54:35: note: directive argument in the range [1, 65535] ../User/Utils/log_error.c:54:5: note: 'snprintf' output between 15 and 1042 bytes into a destination of size 128 54 | snprintf(gStr, LOG_GSTR_SIZE, "%s line:%u hits:%u", | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 | gLogEntrys[i].fileName, gLogEntrys[i].line, gLogEntrys[i].hits); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~A juzgar por los síntomas, creo que su problema es una variante del problema informado a GCC como GCC Bug 95755 , informado en junio de 2020 pero aún no solucionado.
Hay una solución temporal en la respuesta al informe. Debería usar límites en las cadenas de formato ( %*.*s o %.*s con números enteros que especifican la longitud), o necesitaría crear la cadena de formato sobre la marcha con una operación snprintf() creando el formato cuerda y un segundo usándola.