curlx: add local snprintf() helper that always nul-terminates (Windows)

Make the helper use `vsnprintf()` internally on all supported Windows
toolchains (dropping `_snprintf()` and `snprintf()`), ensure to
nul-terminate. Omit the return value to avoid complexity.

Use the helper from `mprintf.c` / `out_double()`, from tests/server code
and the tests/server-specific build of `curlx_inet_ntop()`,
`curlx_strerror()` functions. In the single call (in tests) where the
returned length was used previously, determine it with `strlen()`.

Refs:
https://github.com/libssh2/libssh2/blob/libssh2-1.11.1/src/misc.c#L57-L79
https://learn.microsoft.com/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l
https://learn.microsoft.com/cpp/c-runtime-library/reference/vsnprintf-vsnprintf-vsnprintf-l-vsnwprintf-vsnwprintf-l

Assisted-by: Jay Satiro
Follow-up to fa8bd1cc09 #20761
Follow-up to 8ab468c8aa #15997

Closes #20765
This commit is contained in:
Viktor Szakats 2026-02-27 16:43:16 +01:00
parent b83ade783d
commit 64f28b8f88
No known key found for this signature in database
8 changed files with 76 additions and 20 deletions

View file

@ -40,6 +40,7 @@ CURLX_C = \
../../lib/curlx/inet_pton.c \
../../lib/curlx/multibyte.c \
../../lib/curlx/nonblock.c \
../../lib/curlx/snprintf.c \
../../lib/curlx/strcopy.c \
../../lib/curlx/strerr.c \
../../lib/curlx/strparse.c \

View file

@ -64,9 +64,9 @@ extern const struct entry_s s_entries[];
#include <curlx/curlx.h>
/* adjust for old MSVC */
#if defined(_MSC_VER) && (_MSC_VER < 1900)
# define snprintf _snprintf
#ifdef _WIN32
#include <curlx/snprintf.h>
#define snprintf curlx_win32_snprintf
#endif
#ifdef _WIN32

View file

@ -606,8 +606,9 @@ static int validate_access(struct testcase *test,
if(!strncmp("verifiedserver", filename, 14)) {
char weare[128];
size_t count = snprintf(weare, sizeof(weare), "WE ROOLZ: %ld\r\n",
(long)our_getpid());
size_t count;
snprintf(weare, sizeof(weare), "WE ROOLZ: %ld\r\n", (long)our_getpid());
count = strlen(weare);
logmsg("Are-we-friendly question received");
test->buffer = curlx_strdup(weare);