Skip to content

Commit 0904640

Browse files
author
Zhou Xiao
committed
fix(ble): fixed printf va list cross function pass failure
1 parent 1c81d11 commit 0904640

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

components/bt/common/ble_log/ble_log_spi_out.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ static void spi_out_log_flush(void);
183183
static int spi_out_ul_log_init(void);
184184
static void spi_out_ul_log_deinit(void);
185185
static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t len, bool with_ts);
186-
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args);
186+
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args, int offset);
187187

188188
#if SPI_OUT_LL_ENABLED
189189
static int spi_out_ll_log_init(void);
@@ -589,12 +589,14 @@ static void spi_out_ul_log_write(uint8_t source, const uint8_t *addr, uint16_t l
589589
spi_out_log_cb_write_loss(ul_log_cb);
590590
}
591591

592-
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args)
592+
static bool spi_out_ul_log_printf(uint8_t source, const char *format, va_list args, int offset)
593593
{
594-
int len = vsnprintf((char *)ul_log_str_buf, SPI_OUT_UL_LOG_STR_BUF_SIZE, format, args);
594+
int len = vsnprintf((char *)(ul_log_str_buf + offset),
595+
SPI_OUT_UL_LOG_STR_BUF_SIZE - offset, format, args);
595596
if (len < 0) {
596597
return false;
597598
}
599+
len += offset;
598600

599601
// Truncate string if overflowed
600602
if (len >= SPI_OUT_UL_LOG_STR_BUF_SIZE) {
@@ -1103,16 +1105,23 @@ int ble_log_spi_out_printf(uint8_t source, const char *format, ...)
11031105
return -1;
11041106
}
11051107

1108+
if (!format) {
1109+
return -1;
1110+
}
1111+
11061112
// Get arguments
11071113
va_list args;
11081114
va_start(args, format);
11091115

1116+
va_list args_copy;
1117+
va_copy(args_copy, args);
1118+
11101119
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1111-
bool ret = spi_out_ul_log_printf(source, format, args);
1120+
bool ret = spi_out_ul_log_printf(source, format, args_copy, 0);
11121121
xSemaphoreGive(ul_log_mutex);
11131122

1123+
va_end(args_copy);
11141124
va_end(args);
1115-
11161125
return ret? 0: -1;
11171126
}
11181127

@@ -1122,18 +1131,29 @@ int ble_log_spi_out_printf_enh(uint8_t source, uint8_t level, const char *tag, c
11221131
return -1;
11231132
}
11241133

1134+
if (!tag || !format) {
1135+
return -1;
1136+
}
1137+
11251138
va_list args;
11261139
va_start(args, format);
11271140

1141+
va_list args_copy;
1142+
va_copy(args_copy, args);
1143+
11281144
// Create log prefix in the format: "[level][tag] "
1145+
bool ret = false;
11291146
xSemaphoreTake(ul_log_mutex, portMAX_DELAY);
1130-
snprintf((char *)ul_log_str_buf, SPI_OUT_UL_LOG_STR_BUF_SIZE,
1131-
"[%d][%s] %s", level, tag? tag: "NULL", format);
1132-
bool ret = spi_out_ul_log_printf(source, (const char *)ul_log_str_buf, args);
1147+
int prefix_len = snprintf((char *)ul_log_str_buf, SPI_OUT_UL_LOG_STR_BUF_SIZE,
1148+
"[%d][%s]", level, tag? tag: "NULL");
1149+
if ((prefix_len < 0) || (prefix_len >= SPI_OUT_UL_LOG_STR_BUF_SIZE)) {
1150+
goto exit;
1151+
}
1152+
ret = spi_out_ul_log_printf(source, format, args_copy, prefix_len);
1153+
exit:
11331154
xSemaphoreGive(ul_log_mutex);
1134-
1155+
va_end(args_copy);
11351156
va_end(args);
1136-
11371157
return ret? 0: -1;
11381158
}
11391159

0 commit comments

Comments
 (0)