diff options
author | Michael Pavone <pavone@retrodev.com> | 2017-03-04 19:02:53 -0800 |
---|---|---|
committer | Michael Pavone <pavone@retrodev.com> | 2017-03-04 19:02:53 -0800 |
commit | 231ee5b4c0c60c93a2227b7d1649559b8fcd7ec9 (patch) | |
tree | ac384c489163da6c9e4a147dbaf4b4e6f32d15cf /util.c | |
parent | 087a93bc6df6408b89577a5d159fdb355dd43daa (diff) |
Fix info_message/warning/error functions to deal with limitations of vsnprintf on Windows
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 42 |
1 files changed, 30 insertions, 12 deletions
@@ -192,11 +192,17 @@ void fatal_error(char *format, ...) va_start(args, format); if (!headless) { //take a guess at the final size - size_t size = strlen(format) * 2; + int32_t size = strlen(format) * 2; char *buf = malloc(size); - size_t actual = vsnprintf(buf, size, format, args); - if (actual >= size) { - actual++; + int32_t actual = vsnprintf(buf, size, format, args); + if (actual >= size || actual < 0) { + if (actual < 0) { + //seems on windows, vsnprintf is returning -1 when the buffer is too small + //since we don't know the proper size, a generous multiplier will hopefully suffice + actual = size * 4; + } else { + actual++; + } free(buf); buf = malloc(actual); va_end(args); @@ -222,11 +228,17 @@ void warning(char *format, ...) warning_printf(format, args); } else { #endif - size_t size = strlen(format) * 2; + int32_t size = strlen(format) * 2; char *buf = malloc(size); - size_t actual = vsnprintf(buf, size, format, args); - if (actual >= size) { - actual++; + int32_t actual = vsnprintf(buf, size, format, args); + if (actual >= size || actual < 0) { + if (actual < 0) { + //seems on windows, vsnprintf is returning -1 when the buffer is too small + //since we don't know the proper size, a generous multiplier will hopefully suffice + actual = size * 4; + } else { + actual++; + } free(buf); buf = malloc(actual); va_end(args); @@ -251,11 +263,17 @@ void info_message(char *format, ...) info_printf(format, args); } else { #endif - size_t size = strlen(format) * 2; + int32_t size = strlen(format) * 2; char *buf = malloc(size); - size_t actual = vsnprintf(buf, size, format, args); - if (actual >= size) { - actual++; + int32_t actual = vsnprintf(buf, size, format, args); + if (actual >= size || actual < 0) { + if (actual < 0) { + //seems on windows, vsnprintf is returning -1 when the buffer is too small + //since we don't know the proper size, a generous multiplier will hopefully suffice + actual = size * 4; + } else { + actual++; + } free(buf); buf = malloc(actual); va_end(args); |