diff --git a/src/base/log.cpp b/src/base/log.cpp index 1cd0ce30a..0f56460b8 100644 --- a/src/base/log.cpp +++ b/src/base/log.cpp @@ -116,7 +116,7 @@ void log_log_impl(LEVEL level, bool have_color, LOG_COLOR color, const char *sys #pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif #if defined(CONF_FAMILY_WINDOWS) - _vsnprintf(pMessage, MessageSize, fmt, args); + _vsprintf_p(pMessage, MessageSize, fmt, args); #else vsnprintf(pMessage, MessageSize, fmt, args); #endif diff --git a/src/base/system.cpp b/src/base/system.cpp index f61644174..ac345d236 100644 --- a/src/base/system.cpp +++ b/src/base/system.cpp @@ -2657,7 +2657,7 @@ int str_format(char *buffer, int buffer_size, const char *format, ...) #if defined(CONF_FAMILY_WINDOWS) va_list ap; va_start(ap, format); - _vsnprintf(buffer, buffer_size, format, ap); + _vsprintf_p(buffer, buffer_size, format, ap); va_end(ap); buffer[buffer_size - 1] = 0; /* assure null termination */ diff --git a/src/test/str.cpp b/src/test/str.cpp index cd38519d2..8304a0d08 100644 --- a/src/test/str.cpp +++ b/src/test/str.cpp @@ -217,6 +217,27 @@ TEST(Str, Endswith) str_length(ABCDEFG) - str_length(DEFG)); } +TEST(StrFormat, Positional) +{ + char aBuf[256]; + + // normal + str_format(aBuf, sizeof(aBuf), "%s %s", "first", "second"); + EXPECT_STREQ(aBuf, "first second"); + + // normal with positional arguments + str_format(aBuf, sizeof(aBuf), "%1$s %2$s", "first", "second"); + EXPECT_STREQ(aBuf, "first second"); + + // reverse + str_format(aBuf, sizeof(aBuf), "%2$s %1$s", "first", "second"); + EXPECT_STREQ(aBuf, "second first"); + + // duplicate + str_format(aBuf, sizeof(aBuf), "%1$s %1$s %2$d %1$s %2$d", "str", 1); + EXPECT_STREQ(aBuf, "str str 1 str 1"); +} + TEST(Str, EndswithNocase) { EXPECT_TRUE(str_endswith_nocase("abcdef", "deF"));