Skip to content

Commit 7b7f9b4

Browse files
committed
Fix MSVC _vsnprintf_s usage
1 parent d5c99d9 commit 7b7f9b4

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

c89stringutils/c89stringutils_string_extras.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
8181
va_list args) {
8282
int rc;
8383
#if defined(_MSC_VER)
84+
if (buffer == NULL || count == 0) {
85+
rc = _vscprintf(format, args);
86+
if (rc < 0) {
87+
LOG_DEBUG("_vscprintf failed with rc=%d", rc);
88+
}
89+
return rc;
90+
}
8491
rc = _vsnprintf_s(buffer, count, _TRUNCATE, format, args);
8592
if (rc < 0) {
8693
rc = _vscprintf(format, args);
@@ -98,7 +105,7 @@ static int wtf_vsnprintf(char *buffer, size_t count, const char *format,
98105
#endif
99106
/* In the case where the string entirely filled the buffer, vsnprintf will
100107
not null-terminate it, but vsnprintf must. */
101-
if (count > 0)
108+
if (count > 0 && buffer != NULL)
102109
buffer[count - 1] = '\0';
103110
return rc;
104111
}

0 commit comments

Comments
 (0)