From 599f3ed9963c52be0122b6a5f01b33a13cdcbdda Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 22 Jun 2024 12:14:41 +0200 Subject: [PATCH] vzsnprintf: New module. * lib/stdio.in.h (vzsnprintf): New declaration, based on lib/vsnprintf.c. * lib/vzsnprintf.c: New file, based on lib/vsnprintf.c. * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize GNULIB_VZSNPRINTF. * modules/stdio (Makefile.am): Substitute GNULIB_VZSNPRINTF. * modules/vzsnprintf: New file. --- ChangeLog | 11 ++++++++ lib/stdio.in.h | 19 ++++++++++++++ lib/vzsnprintf.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++ m4/stdio_h.m4 | 3 ++- modules/stdio | 1 + modules/vzsnprintf | 27 +++++++++++++++++++ 6 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 lib/vzsnprintf.c create mode 100644 modules/vzsnprintf diff --git a/ChangeLog b/ChangeLog index 8e82912063..60ed445021 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-06-22 Bruno Haible + + vzsnprintf: New module. + * lib/stdio.in.h (vzsnprintf): New declaration, based on + lib/vsnprintf.c. + * lib/vzsnprintf.c: New file, based on lib/vsnprintf.c. + * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize + GNULIB_VZSNPRINTF. + * modules/stdio (Makefile.am): Substitute GNULIB_VZSNPRINTF. + * modules/vzsnprintf: New file. + 2024-06-20 Bruno Haible test-framework-sh: Fix side effect on dfa tests (regression 2024-06-11). diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 1c0c9661bf..d8d27a56c5 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -1769,6 +1769,25 @@ _GL_CXXALIASWARN (vscanf); # endif #endif +#if @GNULIB_VZSNPRINTF@ +/* Prints formatted output to string STR. Similar to sprintf, 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 -1 with errno set. + Failure code EOVERFLOW can only occur when a width > INT_MAX is used. + Therefore, if the format string is valid and does not use %ls/%lc + directives nor widths, the only possible failure code is ENOMEM. */ +_GL_FUNCDECL_SYS (vzsnprintf, ptrdiff_t, + (char *restrict str, size_t size, + const char *restrict format, va_list args) + _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (3, 0) + _GL_ARG_NONNULL ((3))); +_GL_CXXALIAS_SYS (vzsnprintf, ptrdiff_t, + (char *restrict str, size_t size, + const char *restrict format, va_list args)); +#endif + #if @GNULIB_VSNPRINTF@ # if @REPLACE_VSNPRINTF@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) diff --git a/lib/vzsnprintf.c b/lib/vzsnprintf.c new file mode 100644 index 0000000000..96e240bdb7 --- /dev/null +++ b/lib/vzsnprintf.c @@ -0,0 +1,65 @@ +/* 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 + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + This file is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +/* Specification. */ +#include + +#include +#include +#include +#include +#include + +#include "vasnprintf.h" + +ptrdiff_t +vzsnprintf (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); + } + + if (len > PTRDIFF_MAX) + { + errno = ENOMEM; + return -1; + } + + return len; +} diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index 8eb5816ad7..9c631f172f 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -1,5 +1,5 @@ # stdio_h.m4 -# serial 63 +# serial 64 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -186,6 +186,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS], gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VPRINTF_POSIX]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSNPRINTF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VSPRINTF_POSIX]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSNPRINTF]) dnl Support Microsoft deprecated alias function names by default. gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_FCLOSEALL], [1]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_MDA_FDOPEN], [1]) diff --git a/modules/stdio b/modules/stdio index 45ae9824ed..63ea9a908e 100644 --- a/modules/stdio +++ b/modules/stdio @@ -121,6 +121,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) -e 's/@''GNULIB_VPRINTF_POSIX''@/$(GNULIB_VPRINTF_POSIX)/g' \ -e 's/@''GNULIB_VSNPRINTF''@/$(GNULIB_VSNPRINTF)/g' \ -e 's/@''GNULIB_VSPRINTF_POSIX''@/$(GNULIB_VSPRINTF_POSIX)/g' \ + -e 's/@''GNULIB_VZSNPRINTF''@/$(GNULIB_VZSNPRINTF)/g' \ -e 's/@''GNULIB_MDA_FCLOSEALL''@/$(GNULIB_MDA_FCLOSEALL)/g' \ -e 's/@''GNULIB_MDA_FDOPEN''@/$(GNULIB_MDA_FDOPEN)/g' \ -e 's/@''GNULIB_MDA_FILENO''@/$(GNULIB_MDA_FILENO)/g' \ diff --git a/modules/vzsnprintf b/modules/vzsnprintf new file mode 100644 index 0000000000..853d88210d --- /dev/null +++ b/modules/vzsnprintf @@ -0,0 +1,27 @@ +Description: +vzsnprintf() function: print formatted output from an stdarg argument list +to a fixed length string (without INT_MAX limitation) + +Files: +lib/vzsnprintf.c + +Depends-on: +stdio +vasnprintf +errno +stdint + +configure.ac: +gl_STDIO_MODULE_INDICATOR([vzsnprintf]) + +Makefile.am: +lib_SOURCES += vzsnprintf.c + +Include: + + +License: +LGPLv2+ + +Maintainer: +all -- 2.39.5