From: Holger Schemel Date: Fri, 15 Nov 2024 20:12:24 +0000 (+0100) Subject: improved wrapper function for vsnprintf() X-Git-Tag: 4.4.0.0-test-4~59 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=cf0ccc15200e299803968703e442f2102ec62b52;p=rocksndiamonds.git improved wrapper function for vsnprintf() --- 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, ...)