From 587920f982d6c3e777ee1f98ce36b403ee85c565 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 22 Jun 2024 12:16:06 +0200 Subject: [PATCH] vsprintf-posix: Use vzsprintf. * lib/stdio.in.h (vsprintf): Move specification to here. * lib/vsprintf.c: Don't include , vasnprintf.h. (SIZE_MAX): Remove macro. (vsprintf): Implement based on vzsprintf. * modules/vsprintf-posix (Depends-on): Add vzsprintf. Remove vasnprintf. --- ChangeLog | 7 +++++++ lib/stdio.in.h | 3 +++ lib/vsprintf.c | 44 +++++------------------------------------- modules/vsprintf-posix | 2 +- 4 files changed, 16 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 470192143c..c823d86fbc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2024-06-22 Bruno Haible + vsprintf-posix: Use vzsprintf. + * lib/stdio.in.h (vsprintf): Move specification to here. + * lib/vsprintf.c: Don't include , 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. diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 2d7e20e3d1..858ab47736 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -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 diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 181bc9814f..065ed74222 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -25,53 +25,19 @@ #include #include #include -#include -#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; } diff --git a/modules/vsprintf-posix b/modules/vsprintf-posix index a3ef9998ed..2bbb36055b 100644 --- a/modules/vsprintf-posix +++ b/modules/vsprintf-posix @@ -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] -- 2.39.5