]> Savannah Git Hosting - gnulib.git/commitdiff
vsprintf-posix: Use vzsprintf.
authorBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:16:06 +0000 (12:16 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 22 Jun 2024 10:16:16 +0000 (12:16 +0200)
* lib/stdio.in.h (vsprintf): Move specification to here.
* lib/vsprintf.c: Don't include <stdlib.h>, vasnprintf.h.
(SIZE_MAX): Remove macro.
(vsprintf): Implement based on vzsprintf.
* modules/vsprintf-posix (Depends-on): Add vzsprintf. Remove vasnprintf.

ChangeLog
lib/stdio.in.h
lib/vsprintf.c
modules/vsprintf-posix

index 470192143c5a9d0a97c5c1d0d75b1586f6229685..c823d86fbc270fccb80d3becedb15216409671a9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2024-06-22  Bruno Haible  <bruno@clisp.org>
 
+       vsprintf-posix: Use vzsprintf.
+       * lib/stdio.in.h (vsprintf): Move specification to here.
+       * lib/vsprintf.c: Don't include <stdlib.h>, vasnprintf.h.
+       (SIZE_MAX): Remove macro.
+       (vsprintf): Implement based on vzsprintf.
+       * modules/vsprintf-posix (Depends-on): Add vzsprintf. Remove vasnprintf.
+
        vzsprintf: New module.
        * lib/stdio.in.h (vzsprintf): New declaration, based on
        lib/vsprintf.c.
index 2d7e20e3d1dafa371f7bf3006cf4b875319160db..858ab477367e489c3807d7f79e1d85194703c803 100644 (file)
@@ -1872,6 +1872,9 @@ _GL_CXXALIAS_SYS (vzsprintf, ptrdiff_t,
 #endif
 
 #if @GNULIB_VSPRINTF_POSIX@
+/* Prints formatted output to string STR.
+   Returns the string length of the formatted string.  Upon failure,
+   returns a negative value.  */
 # if @REPLACE_VSPRINTF@
 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
 #   define vsprintf rpl_vsprintf
index 181bc9814f27c4cf418486e2610a36d0885f93e7..065ed7422228b65b0b584b3293e081d63569500c 100644 (file)
 #include <limits.h>
 #include <stdarg.h>
 #include <stdint.h>
-#include <stdlib.h>
 
-#include "vasnprintf.h"
-
-#ifndef SIZE_MAX
-# define SIZE_MAX ((size_t) -1)
-#endif
-
-/* Print formatted output to string STR.
-   Return string length of formatted string.  On error, return a negative
-   value.  */
 int
 vsprintf (char *str, const char *format, va_list args)
 {
-  char *output;
-  size_t len;
-  size_t lenbuf;
-
-  /* vasnprintf fails with EOVERFLOW when the buffer size argument is larger
-     than INT_MAX (if that fits into a 'size_t' at all).
-     Also note that glibc's iconv fails with E2BIG when we pass a length that
-     is so large that str + lenbuf wraps around, i.e.
-     (uintptr_t) (str + lenbuf) < (uintptr_t) str.
-     Therefore set lenbuf = min (SIZE_MAX, INT_MAX, - (uintptr_t) str - 1).  */
-  lenbuf = (SIZE_MAX < INT_MAX ? SIZE_MAX : INT_MAX);
-  if (lenbuf > ~ (uintptr_t) str)
-    lenbuf = ~ (uintptr_t) str;
-
-  output = vasnprintf (str, &lenbuf, format, args);
-  len = lenbuf;
-
-  if (!output)
-    return -1;
+  ptrdiff_t ret = vzsprintf (str, format, args);
 
-  if (output != str)
-    {
-      /* len is near SIZE_MAX.  */
-      free (output);
-      errno = EOVERFLOW;
-      return -1;
-    }
-
-  if (len > INT_MAX)
+#if PTRDIFF_MAX > INT_MAX
+  if (ret > INT_MAX)
     {
       errno = EOVERFLOW;
       return -1;
     }
+#endif
 
-  return len;
+  return ret;
 }
index a3ef9998ed403eed6141e2eb9300c7537b43cbe1..2bbb36055b975cb72c7b644e884a0f7a32547ee9 100644 (file)
@@ -18,7 +18,7 @@ stdio
 nocrash
 printf-safe
 multiarch
-vasnprintf      [test $REPLACE_VSPRINTF = 1]
+vzsprintf       [test $REPLACE_VSPRINTF = 1]
 isnand-nolibm   [test $REPLACE_VSPRINTF = 1]
 isnanl-nolibm   [test $REPLACE_VSPRINTF = 1]
 frexp-nolibm    [test $REPLACE_VSPRINTF = 1]