From 7a34ce4ce5652715bd9dc9d2b3a82a2ea97ed4bc Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 22 Jun 2024 12:14:52 +0200 Subject: [PATCH] vsnprintf: Use vzsnprintf. * lib/stdio.in.h (vsnprintf): Move specification to here. * lib/vsnprintf.c: Don't include , , vasnprintf.h. Include . (vsnprintf): Implement based on vzsnprintf. * modules/vsnprintf (Depends-on): Add stdint, vzsnprintf. Remove vasnprintf. --- ChangeLog | 8 ++++++++ lib/stdio.in.h | 5 +++++ lib/vsnprintf.c | 39 ++++++--------------------------------- modules/vsnprintf | 5 +++-- 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/ChangeLog b/ChangeLog index 60ed445021..28833003fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2024-06-22 Bruno Haible + vsnprintf: Use vzsnprintf. + * lib/stdio.in.h (vsnprintf): Move specification to here. + * lib/vsnprintf.c: Don't include , , vasnprintf.h. + Include . + (vsnprintf): Implement based on vzsnprintf. + * modules/vsnprintf (Depends-on): Add stdint, vzsnprintf. Remove + vasnprintf. + vzsnprintf: New module. * lib/stdio.in.h (vzsnprintf): New declaration, based on lib/vsnprintf.c. diff --git a/lib/stdio.in.h b/lib/stdio.in.h index d8d27a56c5..8d6cd21fd5 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -1789,6 +1789,11 @@ _GL_CXXALIAS_SYS (vzsnprintf, ptrdiff_t, #endif #if @GNULIB_VSNPRINTF@ +/* Prints formatted output to string STR. Similar to vsprintf, but the + additional parameter SIZE limits how much is written into STR. + STR may be NULL, in which case nothing will be written. + Returns the string length of the formatted string (which may be larger + than SIZE). Upon failure, returns a negative value. */ # if @REPLACE_VSNPRINTF@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) # define vsnprintf rpl_vsnprintf diff --git a/lib/vsnprintf.c b/lib/vsnprintf.c index e6676a1ffa..1954dcea26 100644 --- a/lib/vsnprintf.c +++ b/lib/vsnprintf.c @@ -1,6 +1,5 @@ /* Formatted output to strings. Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc. - Written by Simon Josefsson and Yoann Vandoorselaere . This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -25,46 +24,20 @@ #include #include #include -#include -#include +#include -#include "vasnprintf.h" - -/* Print formatted output to string STR. Similar to vsprintf, but - additional length SIZE limit how much is written into STR. Returns - string length of formatted string (which may be larger than SIZE). - STR may be NULL, in which case nothing will be written. On error, - return a negative value. */ int vsnprintf (char *str, size_t size, const char *format, va_list args) { - char *output; - size_t len; - size_t lenbuf = size; - - output = vasnprintf (str, &lenbuf, format, args); - len = lenbuf; - - if (!output) - return -1; - - if (output != str) - { - if (size) - { - size_t pruned_len = (len < size ? len : size - 1); - memcpy (str, output, pruned_len); - str[pruned_len] = '\0'; - } - - free (output); - } + ptrdiff_t ret = vzsnprintf (str, size, format, args); - 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/vsnprintf b/modules/vsnprintf index 053b10c32a..de2a86e285 100644 --- a/modules/vsnprintf +++ b/modules/vsnprintf @@ -9,8 +9,9 @@ m4/printf.m4 Depends-on: stdio -vasnprintf [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1] errno [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1] +stdint [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1] +vzsnprintf [test $ac_cv_func_vsnprintf = no || test $REPLACE_VSNPRINTF = 1] configure.ac: gl_FUNC_VSNPRINTF @@ -25,4 +26,4 @@ License: LGPLv2+ Maintainer: -Yoann Vandoorselaere +all -- 2.39.5