summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2017-03-04 19:02:53 -0800
committerMichael Pavone <pavone@retrodev.com>2017-03-04 19:02:53 -0800
commit231ee5b4c0c60c93a2227b7d1649559b8fcd7ec9 (patch)
treeac384c489163da6c9e4a147dbaf4b4e6f32d15cf
parent087a93bc6df6408b89577a5d159fdb355dd43daa (diff)
Fix info_message/warning/error functions to deal with limitations of vsnprintf on Windows
-rw-r--r--util.c42
1 files changed, 30 insertions, 12 deletions
diff --git a/util.c b/util.c
index 59f51b6..a5aae2e 100644
--- a/util.c
+++ b/util.c
@@ -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);