From 1b6ba0bf7fd136ab05c75d952aa62fd450a9ed7b Mon Sep 17 00:00:00 2001 From: Brian Barto Date: Mon, 18 Apr 2016 14:27:41 -0400 Subject: [PATCH] Used snprintf to dynamically grow print buffer size as needed. modified: src/nms.c --- src/nms.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/nms.c b/src/nms.c index 55dbf77..05e012d 100644 --- a/src/nms.c +++ b/src/nms.c @@ -20,6 +20,8 @@ #define NEWLINE 10 #define TAB 9 +#define PRINT_BUFFER 100 + // Window position structure, linked list. Keeps track of every // character's position on the terminal, as well as other attributes. struct winpos { @@ -50,11 +52,16 @@ char getMaskChar(void); * const char *format - printf-style format string */ void nmsprintf(const char *format, ...) { - char nmsprintBuffer[10000]; + int bufferSize = PRINT_BUFFER; + int bufferIncrementSize = PRINT_BUFFER; + char *nmsprintBuffer = malloc(bufferSize); va_list argp; va_start(argp, format); - vsprintf(nmsprintBuffer, format, argp); + while (vsnprintf(nmsprintBuffer, bufferSize, format, argp) >= strlen(nmsprintBuffer) + 1) { + bufferSize += bufferIncrementSize; + nmsprintBuffer = realloc(nmsprintBuffer, bufferSize); + } va_end(argp); if (display == NULL) {