On Sat, 22 Feb 2020, Pali Rohár wrote:

Also another detail I noticed in your patch:

diff --git a/mingw-w64-crt/stdio/snprintf.c b/mingw-w64-crt/stdio/snprintf.c
index 93300113..0bb5556f 100644
--- a/mingw-w64-crt/stdio/snprintf.c
+++ b/mingw-w64-crt/stdio/snprintf.c
@@ -12,7 +12,27 @@ int __cdecl __ms_snprintf(char* buffer, size_t n, const char 
*format, ...)
   va_list argptr;

   va_start(argptr, format);
+
+  /* _vsnprintf() does not work with zero length buffer
+   * so count number of character by _vscprintf() call */
+  if (n == 0 || !buffer)
+  {
+    retval = _vscprintf(format, argptr);
+    va_end(argptr);
+    return retval;
+  }
+
   retval = _vsnprintf (buffer, n, format, argptr);
+
+  /* _vsnprintf() returns negative number if buffer is too small
+   * so count number of character by _vscprintf() call */
+  if (retval < 0)
+    retval = _vscprintf(format, argptr);

Here you're reusing argptr again, after already using it once for _vsnprintf
above. I think the correct solution to that is to make a new va_arg with
va_copy, before calling _vsnprintf.

I was thinking about it prior sending patch. But mingw has already
similar code which does not call va_copy() and also in MS documentation
is similar code without va_copy()
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l
And because it worked without va_copy() I decided to not use it.

But if you think that it is really needed, it should be fixed on all
places in mingw-w64 code where va_list is passed to other functions more
times.

Ok, if we consistently skip it in other places, I guess it's ok this way.

On Windows, va_list is a plain pointer, so reusing it (as it is passed by value to the called functions) should be fine, as long as one keeps in mind that behaviour isn't portable. (On x86_64 linux, va_list is a struct and is passed by reference, so there va_copy is essential.)

// Martin

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to