Skip to content

Commit 6f6fce9

Browse files
xiaoxiang781216jerpelea
authored andcommitted
Replace all sprintf with snprintf
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
1 parent fa8719b commit 6f6fce9

File tree

36 files changed

+120
-84
lines changed

36 files changed

+120
-84
lines changed

arch/arm/src/armv7-m/arm_systick.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ struct timer_lowerhalf_s *systick_initialize(bool coreclk,
302302
{
303303
char devname[32];
304304

305-
sprintf(devname, "/dev/timer%d", minor);
305+
snprintf(devname, sizeof(devname), "/dev/timer%d", minor);
306306
timer_register(devname, (struct timer_lowerhalf_s *)lower);
307307
}
308308

arch/arm/src/armv8-m/arm_systick.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ struct timer_lowerhalf_s *systick_initialize(bool coreclk,
302302
{
303303
char devname[32];
304304

305-
sprintf(devname, "/dev/timer%d", minor);
305+
snprintf(devname, sizeof(devname), "/dev/timer%d", minor);
306306
timer_register(devname, (struct timer_lowerhalf_s *)lower);
307307
}
308308

arch/arm/src/c5471/c5471_watchdog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ static ssize_t wdt_read(struct file *filep, char *buffer, size_t buflen)
250250
wdinfo("buflen=%d\n", buflen);
251251
if (buflen >= 18)
252252
{
253-
sprintf(buffer, "%08" PRIx32 " %08" PRIx32 "\n",
254-
c5471_wdt_cntl, c5471_wdt_count);
253+
snprintf(buffer, buflen, "%08" PRIx32 " %08" PRIx32 "\n",
254+
c5471_wdt_cntl, c5471_wdt_count);
255255
return 18;
256256
}
257257

arch/arm/src/phy62xx/uart.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,15 @@ int hal_uart_set_tx_buf(UART_INDEX_e uart_index, uint8_t *buf,
165165
uint16_t size);
166166
int hal_uart_get_tx_ready(UART_INDEX_e uart_index);
167167
int hal_uart_send_buff(UART_INDEX_e uart_index, uint8_t *buff, uint16_t len);
168-
#define logx(...) {char tmp_str[128]; sprintf(tmp_str, __VA_ARGS__); hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1);}
168+
#define logx(...) \
169+
do \
170+
{ \
171+
char tmp_str[128]; \
172+
snprintf(tmp_str, sizeof(tmp_str), __VA_ARGS__); \
173+
hal_uart_send_buff(0, &tmp_str, strlen(tmp_str) + 1); \
174+
} \
175+
while (0)
176+
169177
int hal_uart_send_byte(UART_INDEX_e uart_index, unsigned char data);
170178
void __attribute__((weak)) hal_UART0_IRQHandler(void);
171179
void __attribute__((weak)) hal_UART1_IRQHandler(void);

arch/arm/src/tlsr82/tlsr82_flash_mtd.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,16 @@ static void tlsr82_flash_print(const char *msg, const uint8_t *buf,
292292
{
293293
if (i % 16 == 0)
294294
{
295-
off += sprintf(&print_buf[off], "0x%08x:", i);
295+
snprintf(&print_buf[off],
296+
sizeof(print_buf) - off, "0x%08x:", i);
297+
off += strlen(&print_buf[off]);
296298
}
297299

298-
off += sprintf(&print_buf[off], "0x%02x ", buf[i]);
299-
i++;
300+
snprintf(&print_buf[off],
301+
sizeof(print_buf) - off, "0x%02x ", buf[i]);
302+
off += strlen(&print_buf[off]);
300303

301-
if (i % 16 == 0)
304+
if (++i % 16 == 0)
302305
{
303306
ferr("%s\n", print_buf);
304307
off = 0;
@@ -320,7 +323,7 @@ static void tlsr82_flash_print(const char *msg, const uint8_t *buf,
320323
static int tlsr82_flash_test(struct tlsr82_flash_dev_s *priv)
321324
{
322325
struct mtd_geometry_s geo;
323-
int ret = OK;
326+
int ret = 0;
324327
int npages = 0;
325328
int i = 0;
326329
int j = 0;
@@ -329,13 +332,16 @@ static int tlsr82_flash_test(struct tlsr82_flash_dev_s *priv)
329332

330333
/* 1. print the manufacture id and unique id */
331334

332-
ret = 0;
333335
ferr("Flash information print:\n");
334336
ferr(" Flash MID: 0x%08lx\n", g_flash_mid);
335-
ret += sprintf(&print_buf[ret], " Flash UID: ");
337+
snprintf(&print_buf[ret],
338+
sizeof(print_buf) - ret, " Flash UID: ");
339+
ret += strlen(&print_buf[ret]);
336340
for (i = 1; i < 16; i++)
337341
{
338-
ret += sprintf(&print_buf[ret], "0x%x ", g_flash_uid[i]);
342+
snprintf(&print_buf[ret],
343+
sizeof(print_buf) - ret, "0x%x ", g_flash_uid[i]);
344+
ret += strlen(&print_buf[ret]);
339345
}
340346

341347
ferr("%s\n", print_buf);

arch/risc-v/src/esp32c3/esp32c3_partition.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ int esp32c3_partition_init(void)
607607
}
608608

609609
strlcpy(label, (char *)info->label, sizeof(label));
610-
sprintf(path, "%s%s", path_base, label);
610+
snprintf(path, sizeof(path), "%s%s", path_base, label);
611611

612612
finfo("INFO: [label]: %s\n", label);
613613
finfo("INFO: [type]: %d\n", info->type);

boards/arm/stm32/cloudctrl/src/stm32_chipid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ const char *stm32_getchipid_string(void)
6363

6464
for (i = 0, c = 0; i < 12; i++)
6565
{
66-
sprintf(&cpuid[c], "%02X", getreg8(0x1ffff7e8 + 11 - i));
66+
snprintf(&cpuid[c], sizeof(cpuid) - c,
67+
"%02X", getreg8(0x1ffff7e8 + 11 - i));
6768
c += 2;
6869
if (i % 4 == 3)
6970
{

boards/arm/stm32/shenzhou/src/stm32_chipid.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ const char *stm32_getchipid_string(void)
6363

6464
for (i = 0, c = 0; i < 12; i++)
6565
{
66-
sprintf(&cpuid[c], "%02X", getreg8(0x1ffff7e8 + 11 - i));
66+
snprintf(&cpuid[c], sizeof(cpuid) - c,
67+
"%02X", getreg8(0x1ffff7e8 + 11 - i));
6768
c += 2;
6869
if (i % 4 == 3)
6970
{

boards/arm/stm32f7/nucleo-144/src/stm32_bringup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ int stm32_bringup(void)
174174
char buf[9];
175175

176176
#ifdef CONFIG_STM32F7_TIM1_QE
177-
sprintf(buf, "/dev/qe0");
177+
snprintf(buf, sizeof(buf), "/dev/qe0");
178178
ret = stm32_qencoder_initialize(buf, 1);
179179
if (ret < 0)
180180
{
@@ -186,7 +186,7 @@ int stm32_bringup(void)
186186
#endif
187187

188188
#ifdef CONFIG_STM32F7_TIM3_QE
189-
sprintf(buf, "/dev/qe2");
189+
snprintf(buf, sizeof(buf), "/dev/qe2");
190190
ret = stm32_qencoder_initialize(buf, 3);
191191
if (ret < 0)
192192
{
@@ -198,7 +198,7 @@ int stm32_bringup(void)
198198
#endif
199199

200200
#ifdef CONFIG_STM32F7_TIM4_QE
201-
sprintf(buf, "/dev/qe3");
201+
snprintf(buf, sizeof(buf), "/dev/qe3");
202202
ret = stm32_qencoder_initialize(buf, 4);
203203
if (ret < 0)
204204
{

boards/arm/stm32l4/nucleo-l432kc/src/stm32_appinit.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ int board_app_initialize(uintptr_t arg)
316316
index = 0;
317317

318318
#ifdef CONFIG_STM32L4_TIM1_QE
319-
sprintf(buf, "/dev/qe%d", index++);
319+
snprintf(buf, sizeof(buf), "/dev/qe%d", index++);
320320
ret = stm32l4_qencoder_initialize(buf, 1);
321321
if (ret != OK)
322322
{
@@ -327,7 +327,7 @@ int board_app_initialize(uintptr_t arg)
327327
#endif
328328

329329
#ifdef CONFIG_STM32L4_TIM2_QE
330-
sprintf(buf, "/dev/qe%d", index++);
330+
snprintf(buf, sizeof(buf), "/dev/qe%d", index++);
331331
ret = stm32l4_qencoder_initialize(buf, 2);
332332
if (ret != OK)
333333
{
@@ -338,7 +338,7 @@ int board_app_initialize(uintptr_t arg)
338338
#endif
339339

340340
#ifdef CONFIG_STM32L4_TIM3_QE
341-
sprintf(buf, "/dev/qe%d", index++);
341+
snprintf(buf, sizeof(buf), "/dev/qe%d", index++);
342342
ret = stm32l4_qencoder_initialize(buf, 3);
343343
if (ret != OK)
344344
{
@@ -349,7 +349,7 @@ int board_app_initialize(uintptr_t arg)
349349
#endif
350350

351351
#ifdef CONFIG_STM32L4_TIM4_QE
352-
sprintf(buf, "/dev/qe%d", index++);
352+
snprintf(buf, sizeof(buf), "/dev/qe%d", index++);
353353
ret = stm32l4_qencoder_initialize(buf, 4);
354354
if (ret != OK)
355355
{
@@ -360,7 +360,7 @@ int board_app_initialize(uintptr_t arg)
360360
#endif
361361

362362
#ifdef CONFIG_STM32L4_TIM5_QE
363-
sprintf(buf, "/dev/qe%d", index++);
363+
snprintf(buf, sizeof(buf), "/dev/qe%d", index++);
364364
ret = stm32l4_qencoder_initialize(buf, 5);
365365
if (ret != OK)
366366
{
@@ -371,7 +371,7 @@ int board_app_initialize(uintptr_t arg)
371371
#endif
372372

373373
#ifdef CONFIG_STM32L4_TIM8_QE
374-
sprintf(buf, "/dev/qe%d", index++);
374+
snprintf(buf, sizeof(buf), "/dev/qe%d", index++);
375375
ret = stm32l4_qencoder_initialize(buf, 8);
376376
if (ret != OK)
377377
{

0 commit comments

Comments
 (0)