From c05c76f1341d1bbb9c4cc69c6885600e763773c0 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 30 Jun 2024 19:47:26 +0200 Subject: [PATCH] fprintf-posix: Use vfzprintf. * lib/stdio.in.h (fprintf): Add specification. * lib/fprintf.c: Don't include , vasnprintf.h. Include intprops.h. (fprintf): Implement based on vfzprintf. * modules/fprintf-posix (Depends-on): Add vfzprintf, intprops. Remove vasnprintf, free-posix. --- ChangeLog | 8 ++++++++ lib/fprintf.c | 34 +++++----------------------------- lib/stdio.in.h | 3 +++ modules/fprintf-posix | 6 +++--- 4 files changed, 19 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index bcbc7bc7c5..8492ed8314 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2024-06-30 Bruno Haible + fprintf-posix: Use vfzprintf. + * lib/stdio.in.h (fprintf): Add specification. + * lib/fprintf.c: Don't include , vasnprintf.h. + Include intprops.h. + (fprintf): Implement based on vfzprintf. + * modules/fprintf-posix (Depends-on): Add vfzprintf, intprops. Remove + vasnprintf, free-posix. + fzprintf: New module. * lib/stdio.in.h (fzprintf): New declaration. * lib/fzprintf.c: New file, based on lib/vfzprintf.c. diff --git a/lib/fprintf.c b/lib/fprintf.c index da4609d8d6..d55f0bafcc 100644 --- a/lib/fprintf.c +++ b/lib/fprintf.c @@ -24,50 +24,26 @@ #include #include #include -#include #include "fseterr.h" -#include "vasnprintf.h" +#include "intprops.h" -/* Print formatted output to the stream FP. - Return string length of formatted string. On error, return a negative - value. */ int fprintf (FILE *fp, const char *format, ...) { - char buf[2000]; - char *output; - size_t len; - size_t lenbuf = sizeof (buf); va_list args; + off64_t ret; va_start (args, format); - output = vasnprintf (buf, &lenbuf, format, args); - len = lenbuf; + ret = vfzprintf (fp, format, args); va_end (args); - if (!output) + if (TYPE_MAXIMUM (off64_t) > INT_MAX && ret > INT_MAX) { fseterr (fp); - return -1; - } - - if (fwrite (output, 1, len, fp) < len) - { - if (output != buf) - free (output); - return -1; - } - - if (output != buf) - free (output); - - if (len > INT_MAX) - { errno = EOVERFLOW; - fseterr (fp); return -1; } - return len; + return ret; } diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 4ce51a743c..1c48954d81 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -583,6 +583,9 @@ _GL_CXXALIAS_SYS (fzprintf, off64_t, #endif #if @GNULIB_FPRINTF_POSIX@ || @GNULIB_FPRINTF@ +/* Prints formatted output to stream FP. + Returns the number of bytes written to the stream. Upon failure, + returns a negative value with the stream's error indicator set. */ # if (@GNULIB_FPRINTF_POSIX@ && @REPLACE_FPRINTF@) \ || (@GNULIB_FPRINTF@ && @REPLACE_STDIO_WRITE_FUNCS@ && (@GNULIB_STDIO_H_NONBLOCKING@ || @GNULIB_STDIO_H_SIGPIPE@)) # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/modules/fprintf-posix b/modules/fprintf-posix index 6cda1ccef7..3ccb9be175 100644 --- a/modules/fprintf-posix +++ b/modules/fprintf-posix @@ -19,10 +19,10 @@ nocrash printf-safe errno multiarch -fseterr [test $REPLACE_FPRINTF = 1] -vasnprintf [test $REPLACE_FPRINTF = 1] +vfzprintf [test $REPLACE_FPRINTF = 1] mixin/printf-posix [test $REPLACE_FPRINTF = 1] -free-posix [test $REPLACE_FPRINTF = 1] +intprops [test $REPLACE_FPRINTF = 1] +fseterr [test $REPLACE_FPRINTF = 1] configure.ac: gl_FUNC_FPRINTF_POSIX -- 2.39.5