From: Andrii Simiklit <andrii.simik...@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.

v5: remove _mesa_*snprintf functions and replace it by util_*snprintf

So use cross platform implementation 'util_vsnprintf'.

Reviewed-by: Emil Velikov <emil.veli...@collabora.com>
Fixes: 2353e96c320d "Coerce vsnprintf to _vsnprintf for windows"
Signed-off-by: Andrii Simiklit <andrii.simik...@globallogic.com>

---
 src/mapi/glapi/gen/gl_enums.py      |  3 ++-
 src/mesa/drivers/common/meta.c      |  5 +++--
 src/mesa/main/debug.c               |  7 ++++---
 src/mesa/main/dlist.c               |  3 ++-
 src/mesa/main/errors.c              | 19 ++++++++++---------
 src/mesa/main/imports.c             | 26 --------------------------
 src/mesa/main/imports.h             |  5 -----
 src/mesa/main/teximage.c            |  7 ++++---
 src/mesa/main/uniform_query.cpp     |  7 ++++---
 src/mesa/main/version.c             | 17 +++++++++--------
 src/mesa/program/prog_instruction.c |  3 ++-
 src/mesa/program/prog_print.c       |  7 ++++---
 src/mesa/program/program_parse.y    | 17 +++++++++--------
 src/util/u_string.h                 |  4 ++++
 14 files changed, 57 insertions(+), 73 deletions(-)

diff --git a/src/mapi/glapi/gen/gl_enums.py b/src/mapi/glapi/gen/gl_enums.py
index 00f8134..da0e9f3 100644
--- a/src/mapi/glapi/gen/gl_enums.py
+++ b/src/mapi/glapi/gen/gl_enums.py
@@ -53,6 +53,7 @@ class PrintGlEnums(gl_XML.gl_print_base):
         print('#include "main/glheader.h"')
         print('#include "main/enums.h"')
         print('#include "main/imports.h"')
+        print('#include "util/u_string.h"')
         print('#include "main/mtypes.h"')
         print('')
         print('typedef struct PACKED {')
@@ -103,7 +104,7 @@ _mesa_enum_to_string(int nr)
    }
    else {
       /* this is not re-entrant safe, no big deal here */
-      _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
+      util_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
       token_tmp[sizeof(token_tmp) - 1] = '\\0';
       return token_tmp;
    }
diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
index 04752e0..de2442b 100644
--- a/src/mesa/drivers/common/meta.c
+++ b/src/mesa/drivers/common/meta.c
@@ -87,6 +87,7 @@
 #include "main/glformats.h"
 #include "util/bitscan.h"
 #include "util/ralloc.h"
+#include "util/u_string.h"
 #include "compiler/nir/nir.h"
 
 /** Return offset in bytes of the field within a vertex struct */
@@ -2045,7 +2046,7 @@ init_draw_stencil_pixels(struct gl_context *ctx)
       texTarget = "RECT";
    else
       texTarget = "2D";
-   _mesa_snprintf(program2, sizeof(program2), program, texTarget);
+   util_snprintf(program2, sizeof(program2), program, texTarget);
 
    _mesa_GenProgramsARB(1, &drawpix->StencilFP);
    _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
@@ -2079,7 +2080,7 @@ init_draw_depth_pixels(struct gl_context *ctx)
       texTarget = "RECT";
    else
       texTarget = "2D";
-   _mesa_snprintf(program2, sizeof(program2), program, texTarget);
+   util_snprintf(program2, sizeof(program2), program, texTarget);
 
    _mesa_GenProgramsARB(1, &drawpix->DepthFP);
    _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
diff --git a/src/mesa/main/debug.c b/src/mesa/main/debug.c
index b1fa1f0..1e193ec 100644
--- a/src/mesa/main/debug.c
+++ b/src/mesa/main/debug.c
@@ -37,6 +37,7 @@
 #include "pixelstore.h"
 #include "readpix.h"
 #include "texobj.h"
+#include "util/u_string.h"
 
 
 static const char *
@@ -285,7 +286,7 @@ write_texture_image(struct gl_texture_object *texObj,
                                  GL_RGBA, GL_UNSIGNED_BYTE, buffer, img);
 
       /* make filename */
-      _mesa_snprintf(s, sizeof(s), "/tmp/tex%u.l%u.f%u.ppm", texObj->Name, 
level, face);
+      util_snprintf(s, sizeof(s), "/tmp/tex%u.l%u.f%u.ppm", texObj->Name, 
level, face);
 
       printf("  Writing image level %u to %s\n", level, s);
       write_ppm(s, buffer, img->Width, img->Height, 4, 0, 1, 2, GL_FALSE);
@@ -331,8 +332,8 @@ _mesa_write_renderbuffer_image(const struct gl_renderbuffer 
*rb)
                           format, type, &ctx->DefaultPacking, buffer);
 
    /* make filename */
-   _mesa_snprintf(s, sizeof(s), "/tmp/renderbuffer%u.ppm", rb->Name);
-   _mesa_snprintf(s, sizeof(s), "C:\\renderbuffer%u.ppm", rb->Name);
+   util_snprintf(s, sizeof(s), "/tmp/renderbuffer%u.ppm", rb->Name);
+   util_snprintf(s, sizeof(s), "C:\\renderbuffer%u.ppm", rb->Name);
 
    printf("  Writing renderbuffer image to %s\n", s);
 
diff --git a/src/mesa/main/dlist.c b/src/mesa/main/dlist.c
index ae23d29..50e6f5c 100644
--- a/src/mesa/main/dlist.c
+++ b/src/mesa/main/dlist.c
@@ -71,6 +71,7 @@
 
 #include "vbo/vbo.h"
 
+#include "util/u_string.h"
 
 #define USE_BITMAP_ATLAS 1
 
@@ -10736,7 +10737,7 @@ execute_list(struct gl_context *ctx, GLuint list)
          default:
             {
                char msg[1000];
-               _mesa_snprintf(msg, sizeof(msg), "Error in execute_list: 
opcode=%d",
+               util_snprintf(msg, sizeof(msg), "Error in execute_list: 
opcode=%d",
                              (int) opcode);
                _mesa_problem(ctx, "%s", msg);
             }
diff --git a/src/mesa/main/errors.c b/src/mesa/main/errors.c
index a968791..8e40eee 100644
--- a/src/mesa/main/errors.c
+++ b/src/mesa/main/errors.c
@@ -35,6 +35,7 @@
 #include "imports.h"
 #include "context.h"
 #include "debug_output.h"
+#include "util/u_string.h"
 
 
 static FILE *LogFile = NULL;
@@ -86,7 +87,7 @@ output_if_debug(const char *prefixString, const char 
*outputString,
        * visible, so communicate with the debugger instead */ 
       {
          char buf[4096];
-         _mesa_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, 
outputString, newline ? "\n" : "");
+         util_snprintf(buf, sizeof(buf), "%s: %s%s", prefixString, 
outputString, newline ? "\n" : "");
          OutputDebugStringA(buf);
       }
 #endif
@@ -116,7 +117,7 @@ flush_delayed_errors( struct gl_context *ctx )
    char s[MAX_DEBUG_MESSAGE_LENGTH];
 
    if (ctx->ErrorDebugCount) {
-      _mesa_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
+      util_snprintf(s, MAX_DEBUG_MESSAGE_LENGTH, "%d similar %s errors",
                      ctx->ErrorDebugCount,
                      _mesa_enum_to_string(ctx->ErrorValue));
 
@@ -140,7 +141,7 @@ _mesa_warning( struct gl_context *ctx, const char 
*fmtString, ... )
    char str[MAX_DEBUG_MESSAGE_LENGTH];
    va_list args;
    va_start( args, fmtString );
-   (void) _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
+   (void) util_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
    va_end( args );
 
    if (ctx)
@@ -170,7 +171,7 @@ _mesa_problem( const struct gl_context *ctx, const char 
*fmtString, ... )
       numCalls++;
 
       va_start( args, fmtString );
-      _mesa_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
+      util_vsnprintf( str, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args );
       va_end( args );
       fprintf(stderr, "Mesa " PACKAGE_VERSION " implementation error: %s\n",
               str);
@@ -230,7 +231,7 @@ _mesa_gl_vdebug(struct gl_context *ctx,
 
    _mesa_debug_get_id(id);
 
-   len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   len = util_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
 
    _mesa_log_msg(ctx, source, type, *id, severity, len, s);
 }
@@ -295,7 +296,7 @@ _mesa_error( struct gl_context *ctx, GLenum error, const 
char *fmtString, ... )
       va_list args;
 
       va_start(args, fmtString);
-      len = _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+      len = util_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
       va_end(args);
 
       if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
@@ -306,7 +307,7 @@ _mesa_error( struct gl_context *ctx, GLenum error, const 
char *fmtString, ... )
          return;
       }
 
-      len = _mesa_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
+      len = util_snprintf(s2, MAX_DEBUG_MESSAGE_LENGTH, "%s in %s",
                            _mesa_enum_to_string(error), s);
       if (len >= MAX_DEBUG_MESSAGE_LENGTH) {
          /* Same as above. */
@@ -352,7 +353,7 @@ _mesa_debug( const struct gl_context *ctx, const char 
*fmtString, ... )
    char s[MAX_DEBUG_MESSAGE_LENGTH];
    va_list args;
    va_start(args, fmtString);
-   _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   util_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
    va_end(args);
    output_if_debug("Mesa", s, GL_FALSE);
 #endif /* DEBUG */
@@ -367,7 +368,7 @@ _mesa_log(const char *fmtString, ...)
    char s[MAX_DEBUG_MESSAGE_LENGTH];
    va_list args;
    va_start(args, fmtString);
-   _mesa_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
+   util_vsnprintf(s, MAX_DEBUG_MESSAGE_LENGTH, fmtString, args);
    va_end(args);
    output_if_debug("", s, GL_FALSE);
 }
diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c
index b4685b6..438068b 100644
--- a/src/mesa/main/imports.c
+++ b/src/mesa/main/imports.c
@@ -58,11 +58,6 @@
 #endif
 
 
-#ifdef _WIN32
-#define vsnprintf _vsnprintf
-#elif defined(__IBMC__) || defined(__IBMCPP__)
-extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
-#endif
 
 /**********************************************************************/
 /** \name Memory */
@@ -250,24 +245,3 @@ _mesa_bitcount_64(uint64_t n)
 
 /*@}*/
 
-
-/** Needed due to #ifdef's, above. */
-int
-_mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list args)
-{
-   return vsnprintf( str, size, fmt, args);
-}
-
-/** Wrapper around vsnprintf() */
-int
-_mesa_snprintf( char *str, size_t size, const char *fmt, ... )
-{
-   int r;
-   va_list args;
-   va_start( args, fmt );  
-   r = vsnprintf( str, size, fmt, args );
-   va_end( args );
-   return r;
-}
-
-
diff --git a/src/mesa/main/imports.h b/src/mesa/main/imports.h
index a761f01..141ae0e 100644
--- a/src/mesa/main/imports.h
+++ b/src/mesa/main/imports.h
@@ -332,11 +332,6 @@ _mesa_bitcount_64(uint64_t n);
 #endif
 
 
-extern int
-_mesa_snprintf( char *str, size_t size, const char *fmt, ... ) PRINTFLIKE(3, 
4);
-
-extern int
-_mesa_vsnprintf(char *str, size_t size, const char *fmt, va_list arg);
 
 
 #if defined(_WIN32) && !defined(strtok_r)
diff --git a/src/mesa/main/teximage.c b/src/mesa/main/teximage.c
index d45854b..230a116 100644
--- a/src/mesa/main/teximage.c
+++ b/src/mesa/main/teximage.c
@@ -55,6 +55,7 @@
 #include "glformats.h"
 #include "texstore.h"
 #include "pbo.h"
+#include "util/u_string.h"
 
 
 /**
@@ -1911,7 +1912,7 @@ texture_error_check( struct gl_context *ctx,
     * requires GL_OES_texture_float) are filtered elsewhere.
     */
    char bufCallerName[20];
-   _mesa_snprintf(bufCallerName, 20, "glTexImage%dD", dimensions);
+   util_snprintf(bufCallerName, 20, "glTexImage%dD", dimensions);
    if (_mesa_is_gles(ctx) &&
        texture_format_error_check_gles(ctx, format, type,
                                        internalFormat, bufCallerName)) {
@@ -1940,7 +1941,7 @@ texture_error_check( struct gl_context *ctx,
       if (type != GL_UNSIGNED_SHORT_8_8_MESA &&
           type != GL_UNSIGNED_SHORT_8_8_REV_MESA) {
          char message[100];
-         _mesa_snprintf(message, sizeof(message),
+         util_snprintf(message, sizeof(message),
                         "glTexImage%dD(format/type YCBCR mismatch)",
                         dimensions);
          _mesa_error(ctx, GL_INVALID_ENUM, "%s", message);
@@ -1957,7 +1958,7 @@ texture_error_check( struct gl_context *ctx,
       }
       if (border != 0) {
          char message[100];
-         _mesa_snprintf(message, sizeof(message),
+         util_snprintf(message, sizeof(message),
                         "glTexImage%dD(format=GL_YCBCR_MESA and border=%d)",
                         dimensions, border);
          _mesa_error(ctx, GL_INVALID_VALUE, "%s", message);
diff --git a/src/mesa/main/uniform_query.cpp b/src/mesa/main/uniform_query.cpp
index de594fc..1f7132d 100644
--- a/src/mesa/main/uniform_query.cpp
+++ b/src/mesa/main/uniform_query.cpp
@@ -37,6 +37,7 @@
 #include "compiler/glsl/glsl_parser_extras.h"
 #include "compiler/glsl/program.h"
 #include "util/bitscan.h"
+#include "util/u_string.h"
 
 
 extern "C" void GLAPIENTRY
@@ -1594,9 +1595,9 @@ _mesa_sampler_uniforms_are_valid(const struct 
gl_shader_program *shProg,
       return true;
 
    if (!shProg->SamplersValidated) {
-      _mesa_snprintf(errMsg, errMsgLength,
-                     "active samplers with a different type "
-                     "refer to the same texture image unit");
+      util_snprintf(errMsg, errMsgLength,
+                    "active samplers with a different type "
+                    "refer to the same texture image unit");
       return false;
    }
    return true;
diff --git a/src/mesa/main/version.c b/src/mesa/main/version.c
index 77ff51b..14d9821 100644
--- a/src/mesa/main/version.c
+++ b/src/mesa/main/version.c
@@ -29,6 +29,7 @@
 #include "mtypes.h"
 #include "version.h"
 #include "git_sha1.h"
+#include "util/u_string.h"
 
 /**
  * Scans 'string' to see if it ends with 'ending'.
@@ -120,14 +121,14 @@ create_version_string(struct gl_context *ctx, const char 
*prefix)
 
    ctx->VersionString = malloc(max);
    if (ctx->VersionString) {
-      _mesa_snprintf(ctx->VersionString, max,
-                    "%s%u.%u%s Mesa " PACKAGE_VERSION MESA_GIT_SHA1,
-                    prefix,
-                    ctx->Version / 10, ctx->Version % 10,
-                    (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" :
-                     (ctx->API == API_OPENGL_COMPAT && ctx->Version >= 32) ?
-                        " (Compatibility Profile)" : ""
-                    );
+      util_snprintf(ctx->VersionString, max,
+                   "%s%u.%u%s Mesa " PACKAGE_VERSION MESA_GIT_SHA1,
+                   prefix,
+                   ctx->Version / 10, ctx->Version % 10,
+                   (ctx->API == API_OPENGL_CORE) ? " (Core Profile)" :
+                    (ctx->API == API_OPENGL_COMPAT && ctx->Version >= 32) ?
+                       " (Compatibility Profile)" : ""
+                   );
    }
 }
 
diff --git a/src/mesa/program/prog_instruction.c 
b/src/mesa/program/prog_instruction.c
index 91830d8..31ffcae 100644
--- a/src/mesa/program/prog_instruction.c
+++ b/src/mesa/program/prog_instruction.c
@@ -26,6 +26,7 @@
 
 #include "main/glheader.h"
 #include "main/imports.h"
+#include "util/u_string.h"
 #include "prog_instruction.h"
 #include "prog_parameter.h"
 
@@ -250,7 +251,7 @@ _mesa_opcode_string(enum prog_opcode opcode)
       return InstInfo[opcode].Name;
    else {
       static char s[20];
-      _mesa_snprintf(s, sizeof(s), "OP%u", opcode);
+      util_snprintf(s, sizeof(s), "OP%u", opcode);
       return s;
    }
 }
diff --git a/src/mesa/program/prog_print.c b/src/mesa/program/prog_print.c
index e8d087c..35830e3 100644
--- a/src/mesa/program/prog_print.c
+++ b/src/mesa/program/prog_print.c
@@ -34,6 +34,7 @@
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/imports.h"
+#include "util/u_string.h"
 #include "prog_instruction.h"
 #include "prog_parameter.h"
 #include "prog_print.h"
@@ -83,7 +84,7 @@ _mesa_register_file_name(gl_register_file f)
    default:
       {
          static char s[20];
-         _mesa_snprintf(s, sizeof(s), "FILE%u", f);
+         util_snprintf(s, sizeof(s), "FILE%u", f);
          return s;
       }
    }
@@ -986,7 +987,7 @@ _mesa_write_shader_to_file(const struct gl_shader *shader)
       break;
    }
 
-   _mesa_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, 
type);
+   util_snprintf(filename, sizeof(filename), "shader_%u.%s", shader->Name, 
type);
    f = fopen(filename, "w");
    if (!f) {
       fprintf(stderr, "Unable to open %s for writing\n", filename);
@@ -1029,7 +1030,7 @@ _mesa_append_uniforms_to_file(const struct gl_program 
*prog)
    else
       type = "vert";
 
-   _mesa_snprintf(filename, sizeof(filename), "shader.%s", type);
+   util_snprintf(filename, sizeof(filename), "shader.%s", type);
    f = fopen(filename, "a"); /* append */
    if (!f) {
       fprintf(stderr, "Unable to open %s for appending\n", filename);
diff --git a/src/mesa/program/program_parse.y b/src/mesa/program/program_parse.y
index 415ff2a..e56eb60 100644
--- a/src/mesa/program/program_parse.y
+++ b/src/mesa/program/program_parse.y
@@ -30,6 +30,7 @@
 #include "main/errors.h"
 #include "main/mtypes.h"
 #include "main/imports.h"
+#include "util/u_string.h"
 #include "program/program.h"
 #include "program/prog_parameter.h"
 #include "program/prog_parameter_layout.h"
@@ -887,8 +888,8 @@ addrRegPosOffset: INTEGER
        {
           if (($1 < 0) || ($1 > (state->limits->MaxAddressOffset - 1))) {
               char s[100];
-              _mesa_snprintf(s, sizeof(s),
-                             "relative address offset too large (%d)", $1);
+              util_snprintf(s, sizeof(s),
+                            "relative address offset too large (%d)", $1);
              yyerror(& @1, state, s);
              YYERROR;
           } else {
@@ -901,8 +902,8 @@ addrRegNegOffset: INTEGER
        {
           if (($1 < 0) || ($1 > state->limits->MaxAddressOffset)) {
               char s[100];
-              _mesa_snprintf(s, sizeof(s),
-                             "relative address offset too large (%d)", $1);
+              util_snprintf(s, sizeof(s),
+                            "relative address offset too large (%d)", $1);
              yyerror(& @1, state, s);
              YYERROR;
           } else {
@@ -1118,9 +1119,9 @@ optArraySize:
         {
           if (($1 < 1) || ((unsigned) $1 > state->limits->MaxParameters)) {
               char msg[100];
-              _mesa_snprintf(msg, sizeof(msg),
-                             "invalid parameter array size (size=%d max=%u)",
-                             $1, state->limits->MaxParameters);
+              util_snprintf(msg, sizeof(msg),
+                            "invalid parameter array size (size=%d max=%u)",
+                            $1, state->limits->MaxParameters);
              yyerror(& @1, state, msg);
              YYERROR;
           } else {
@@ -2031,7 +2032,7 @@ ALIAS_statement: ALIAS IDENTIFIER '=' USED_IDENTIFIER
 
           if (exist != NULL) {
              char m[1000];
-             _mesa_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
+             util_snprintf(m, sizeof(m), "redeclared identifier: %s", $2);
              free($2);
              yyerror(& @2, state, m);
              YYERROR;
diff --git a/src/util/u_string.h b/src/util/u_string.h
index e408146..ca65623 100644
--- a/src/util/u_string.h
+++ b/src/util/u_string.h
@@ -208,6 +208,10 @@ util_strstr(const char *haystack, const char *needle)
 
 #else
 
+#if defined(__IBMC__) || defined(__IBMCPP__)
+extern int vsnprintf(char *str, size_t count, const char *fmt, va_list arg);
+#endif
+
 #define util_vsnprintf vsnprintf
 #define util_snprintf snprintf
 #define util_vsprintf vsprintf
-- 
2.7.4

_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to