From cf0ccc15200e299803968703e442f2102ec62b52 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Fri, 15 Nov 2024 21:12:24 +0100 Subject: [PATCH] improved wrapper function for vsnprintf() --- src/libgame/misc.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 15c46411..5cc80f79 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -1252,11 +1252,20 @@ char *getStringToLower(const char *s) static char *getStringVPrint(char *format, va_list ap) { - char s[MAX_LINE_LEN]; + char test[1]; - vsnprintf(s, MAX_LINE_LEN, format, ap); // may truncate output string + // determine required size of string to be printed + int size = vsnprintf(test, 1, format, ap); - return getStringCopy(s); + // check if something went wrong (should not happen) + if (size < 0) + size = MAX_LINE_LEN; + + char *s = checked_malloc(size + 1); + + vsnprintf(s, size + 1, format, ap); + + return s; } char *getStringPrint(char *format, ...) -- 2.34.1