[v4,6/8] mesa/util: use cross platform implementation 'util_vsnprintf'

Submitted by andrey simiklit on Sept. 6, 2018, 2:43 p.m.

Details

Message ID 1536245039-22231-7-git-send-email-asimiklit.work@gmail.com
State New
Headers show
Series "mesa/util: add missing va_end() after va_copy()" ( rev: 9 ) in Mesa

Not browsing as part of any series.

Commit Message

andrey simiklit Sept. 6, 2018, 2:43 p.m.
From: Andrii Simiklit <andrii.simiklit@globallogic.com>

The win '_vsnprintf' function incompatibility with C99 vsnprintf.
At least for case when the input buffer size less than the required size:
'_vsnprintf' returns -1 for this case.
'vsnprintf' returns the required size.

So use cross platform implementation 'util_vsnprintf'.

Signed-off-by: Andrii Simiklit <andrii.simiklit@globallogic.com>
---
 src/util/ralloc.c   | 25 ++++++-------------------
 src/util/u_string.h |  5 +++++
 2 files changed, 11 insertions(+), 19 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/util/ralloc.c b/src/util/ralloc.c
index 5d77f75..798560f 100644
--- a/src/util/ralloc.c
+++ b/src/util/ralloc.c
@@ -28,13 +28,8 @@ 
 #include <string.h>
 #include <stdint.h>
 
-/* Some versions of MinGW are missing _vscprintf's declaration, although they
- * still provide the symbol in the import library. */
-#ifdef __MINGW32__
-_CRTIMP int _vscprintf(const char *format, va_list argptr);
-#endif
-
 #include "ralloc.h"
+#include "u_string.h"
 
 #ifndef va_copy
 #ifdef __va_copy
@@ -448,15 +443,7 @@  printf_length(const char *fmt, va_list untouched_args)
    va_list args;
    va_copy(args, untouched_args);
 
-#ifdef _WIN32
-   /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
-    * if the number of characters to write is greater than count.
-    */
-   size = _vscprintf(fmt, args);
-   (void)junk;
-#else
-   size = vsnprintf(&junk, 1, fmt, args);
-#endif
+   size = util_vsnprintf(&junk, 1, fmt, args);
    assert(size >= 0);
 
    va_end(args);
@@ -471,7 +458,7 @@  ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
 
    char *ptr = ralloc_size(ctx, size);
    if (ptr != NULL)
-      vsnprintf(ptr, size, fmt, args);
+      util_vsnprintf(ptr, size, fmt, args);
 
    return ptr;
 }
@@ -529,7 +516,7 @@  ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
    if (unlikely(ptr == NULL))
       return false;
 
-   vsnprintf(ptr + *start, new_length + 1, fmt, args);
+   util_vsnprintf(ptr + *start, new_length + 1, fmt, args);
    *str = ptr;
    *start += new_length;
    return true;
@@ -804,7 +791,7 @@  linear_vasprintf(void *parent, const char *fmt, va_list args)
 
    char *ptr = linear_alloc_child(parent, size);
    if (ptr != NULL)
-      vsnprintf(ptr, size, fmt, args);
+      util_vsnprintf(ptr, size, fmt, args);
 
    return ptr;
 }
@@ -862,7 +849,7 @@  linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
    if (unlikely(ptr == NULL))
       return false;
 
-   vsnprintf(ptr + *start, new_length + 1, fmt, args);
+   util_vsnprintf(ptr + *start, new_length + 1, fmt, args);
    *str = ptr;
    *start += new_length;
    return true;
diff --git a/src/util/u_string.h b/src/util/u_string.h
index e408146..29522b2 100644
--- a/src/util/u_string.h
+++ b/src/util/u_string.h
@@ -67,6 +67,11 @@  util_strchrnul(const char *s, char c)
 #endif
 
 #ifdef _WIN32
+/* Some versions of MinGW are missing _vscprintf's declaration, although they
+ * still provide the symbol in the import library. */
+#ifdef __MINGW32__
+_CRTIMP int _vscprintf(const char *format, va_list argptr);
+#endif
 
 static inline int
 util_vsnprintf(char *str, size_t size, const char *format, va_list ap)