From: Bruno Haible Date: Sat, 22 Jun 2024 10:16:24 +0000 (+0200) Subject: zsprintf: New module. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=f89f459b49563a6dfb82b5deac70d87086a390f3;p=gnulib.git zsprintf: New module. * lib/stdio.in.h (zsprintf): New declaration, based on lib/sprintf.c. * lib/zsprintf.c: New file, based on lib/sprintf.c. * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize GNULIB_ZSPRINTF. * modules/stdio (Makefile.am): Substitute GNULIB_ZSPRINTF. * modules/zsprintf: New file. --- diff --git a/ChangeLog b/ChangeLog index c823d86fbc..96dc1dff2c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-06-22 Bruno Haible + + zsprintf: New module. + * lib/stdio.in.h (zsprintf): New declaration, based on + lib/sprintf.c. + * lib/zsprintf.c: New file, based on lib/sprintf.c. + * m4/stdio_h.m4 (gl_STDIO_H_REQUIRE_DEFAULTS): Initialize + GNULIB_ZSPRINTF. + * modules/stdio (Makefile.am): Substitute GNULIB_ZSPRINTF. + * modules/zsprintf: New file. + 2024-06-22 Bruno Haible vsprintf-posix: Use vzsprintf. diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 858ab47736..17c662f45a 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -1494,6 +1494,23 @@ _GL_WARN_ON_USE (snprintf, "snprintf is unportable - " # endif #endif +#if @GNULIB_ZSPRINTF@ +/* Prints formatted output to string STR. + Returns the string length of the formatted string. 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 (zsprintf, ptrdiff_t, + (char *restrict str, + const char *restrict format, ...) + _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3) + _GL_ARG_NONNULL ((1, 2))); +_GL_CXXALIAS_SYS (zsprintf, ptrdiff_t, + (char *restrict str, + const char *restrict format, ...)); +#endif + /* Some people would argue that all sprintf uses should be warned about (for example, OpenBSD issues a link warning for it), since it can cause security holes due to buffer overruns. diff --git a/lib/zsprintf.c b/lib/zsprintf.c new file mode 100644 index 0000000000..f004ed7238 --- /dev/null +++ b/lib/zsprintf.c @@ -0,0 +1,67 @@ +/* Formatted output to strings. + Copyright (C) 2004, 2006-2024 Free Software Foundation, Inc. + + 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 3 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 "vasnprintf.h" + +ptrdiff_t +zsprintf (char *str, const char *format, ...) +{ + char *output; + size_t len; + size_t lenbuf; + va_list args; + + /* Set lenbuf = min (SIZE_MAX, - (uintptr_t) str - 1). */ + lenbuf = SIZE_MAX; + if (lenbuf >= ~ (uintptr_t) str) + lenbuf = ~ (uintptr_t) str; + + va_start (args, format); + output = vasnprintf (str, &lenbuf, format, args); + len = lenbuf; + va_end (args); + + if (!output) + return -1; + + if (output != str) + { + /* len is near SIZE_MAX. */ + free (output); + errno = ENOMEM; + return -1; + } + + if (len > PTRDIFF_MAX) + { + errno = ENOMEM; + return -1; + } + + return len; +} diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index f549ca4810..69d4b88b4b 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -1,5 +1,5 @@ # stdio_h.m4 -# serial 66 +# serial 67 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, @@ -189,6 +189,7 @@ AC_DEFUN([gl_STDIO_H_REQUIRE_DEFAULTS], gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSNPRINTF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_VZSPRINTF]) gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ZSNPRINTF]) + gl_MODULE_INDICATOR_INIT_VARIABLE([GNULIB_ZSPRINTF]) 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 4e41ae13e8..29ed8e0bfa 100644 --- a/modules/stdio +++ b/modules/stdio @@ -124,6 +124,7 @@ stdio.h: stdio.in.h $(top_builddir)/config.status $(CXXDEFS_H) $(ARG_NONNULL_H) -e 's/@''GNULIB_VZSNPRINTF''@/$(GNULIB_VZSNPRINTF)/g' \ -e 's/@''GNULIB_VZSPRINTF''@/$(GNULIB_VZSPRINTF)/g' \ -e 's/@''GNULIB_ZSNPRINTF''@/$(GNULIB_ZSNPRINTF)/g' \ + -e 's/@''GNULIB_ZSPRINTF''@/$(GNULIB_ZSPRINTF)/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/zsprintf b/modules/zsprintf new file mode 100644 index 0000000000..7da8687ea4 --- /dev/null +++ b/modules/zsprintf @@ -0,0 +1,27 @@ +Description: +zsprintf() function: print formatted output to a string (without INT_MAX +limitation) + +Files: +lib/zsprintf.c + +Depends-on: +stdio +vasnprintf +errno +stdint + +configure.ac: +gl_STDIO_MODULE_INDICATOR([zsprintf]) + +Makefile.am: +lib_SOURCES += zsprintf.c + +Include: + + +License: +LGPL + +Maintainer: +all