summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/m_misc.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/m_misc.c b/src/m_misc.c
index 4abd7ebf..d54e181e 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -459,6 +459,8 @@ char *M_StringJoin(const char *s, ...)
// Safe, portable vsnprintf().
int M_vsnprintf(char *buf, size_t buf_len, const char *s, va_list args)
{
+ int result;
+
if (buf_len < 1)
{
return 0;
@@ -467,8 +469,17 @@ int M_vsnprintf(char *buf, size_t buf_len, const char *s, va_list args)
// Windows (and other OSes?) has a vsnprintf() that doesn't always
// append a trailing \0. So we must do it, and write into a buffer
// that is one byte shorter; otherwise this function is unsafe.
- buf[buf_len - 1] = '\0';
- return vsnprintf(buf, buf_len - 1, s, args);
+ result = vsnprintf(buf, buf_len, s, args);
+
+ // If truncated, change the final char in the buffer to a \0.
+ // A negative result indicates a truncated buffer on Windows.
+ if (result < 0 || result >= buf_len)
+ {
+ buf[buf_len - 1] = '\0';
+ result = buf_len - 1;
+ }
+
+ return result;
}
// Safe, portable snprintf().