Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

324
Views
No se puede resolver la advertencia de snprintf en C

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); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!