changeset 671:2bee2b6b6386

asprintf: fix Windows impl
author David Demelier <markand@malikania.fr>
date Thu, 24 Mar 2022 07:29:22 +0100
parents 2d4a781b517c
children c49478852aaf
files c/asprintf/asprintf.c
diffstat 1 files changed, 3 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/c/asprintf/asprintf.c	Thu Mar 24 07:22:08 2022 +0100
+++ b/c/asprintf/asprintf.c	Thu Mar 24 07:29:22 2022 +0100
@@ -25,10 +25,6 @@
  * The Microsoft implementation relies on the Win32 API function
  * _vscprintf which count the number of characters needed. Otherwise the C99
  * function vsnprintf returns the number of character that would be printed.
- *
- * Finally, if we don't have C99, we use an optimistic function.
- *
- * The asprintf function is common to every implementations.
  */
 
 #if defined(_MSC_VER)
@@ -46,13 +42,13 @@
 		return -1;
 	}
 
-	if ((*res = (char *)malloc(sizeof (total) + 1)) == NULL)
+	if (!(*res = malloc(total + 1)))
 		return -1;
 
 	return vsprintf_s(*res, total + 1, fmt, ap);
 }
 
-#elif __STDC_VERSION__ >= 199901L
+#else
 
 /*
  * C99 implementation using vsnprintf's return value.
@@ -72,7 +68,7 @@
 		return total;
 	}
 
-	if ((*res = malloc(total + 1)) == NULL)
+	if (!(*res = malloc(total + 1)))
 		return -1;
 
 	if ((nwritten = vsnprintf(*res, total + 1, fmt, ap)) < 0) {
@@ -85,36 +81,6 @@
 	return nwritten;
 }
 
-#else
-
-/*
- * Optimistic function fallback.
- */
-int
-vasprintf(char **res, const char *format, va_list ap)
-{
-	int rvalue, ok;
-	size_t base = 80;
-
-	if ((*res = malloc(base)) == NULL)
-		return -1;
-
-	ok = 0;
-	do {
-		rvalue = vsnprintf(*res, base, format, ap);
-
-		if ((signed int)base <= rvalue || rvalue < 0) {
-			*res = realloc(*res, base * 2);
-			base *= 2;
-		} else
-			ok = 1;
-	} while (!ok && *res != NULL);
-
-	return rvalue;
-}
-
-#endif
-
 int
 asprintf(char **res, const char *fmt, ...)
 {