[PATCH v2 2/3] stdio: Add vstpeprintf()

Alejandro Colomar alx.manpages@gmail.com
Wed Dec 28 23:17:41 GMT 2022


[v]snprintf(3) is error-prone, since it doesn't allow easy catenation or
chaining.  To catenate a formatted string after an existing string, the
only possibility was to call [v]snprintf(3), adjusting the sizes and
pointers.  However, that is very error-prone, and has caused several
bugs in existing software.  I found several just in a small
investigation in some noteworthy open-source projects.

This API solves that problem by receiving a pointer to the end of the
destination buffer, so there's no recalculation involved.  It also
always returns a pointer suitable for chaining with other calls to this
function, or calls to stpecpy(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 libio/Makefile      |  4 ++--
 libio/stdio.h       |  6 ++++++
 libio/vstpeprintf.c | 52 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+), 2 deletions(-)
 create mode 100644 libio/vstpeprintf.c

diff --git a/libio/Makefile b/libio/Makefile
index 64398ab1ee..1924f7a65c 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -43,8 +43,8 @@ routines	:=							      \
 									      \
 	clearerr feof ferror fileno fputc freopen fseek getc getchar	      \
 	memstream pclose putc putchar rewind setbuf setlinebuf vasprintf      \
-	iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello	      \
-	freopen64 fseeko64 ftello64					      \
+	iovdprintf vscanf vsnprintf vstpeprintf obprintf fcloseall	      \
+	fseeko ftello freopen64 fseeko64 ftello64			      \
 									      \
 	__fbufsize __freading __fwriting __freadable __fwritable __flbf	      \
 	__fpurge __fpending __fsetlocking				      \
diff --git a/libio/stdio.h b/libio/stdio.h
index 0e0f16b464..59b8047ecc 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -384,6 +384,12 @@ extern int vsnprintf (char *__restrict __s, size_t __maxlen,
      __THROWNL __attribute__ ((__format__ (__printf__, 3, 0)));
 #endif
 
+#if __USE_GNU
+extern char *vstpeprintf (char *__dest, char *__end,
+			  const char *__restrict __fmt, __gnuc_va_list __arg)
+     __THROWNL __attribute__ ((__format__ (__printf__, 3, 0)));
+#endif
+
 #if __GLIBC_USE (LIB_EXT2)
 /* Write formatted output to a string dynamically allocated with `malloc'.
    Store the address of the string in *PTR.  */
diff --git a/libio/vstpeprintf.c b/libio/vstpeprintf.c
new file mode 100644
index 0000000000..a8becdc682
--- /dev/null
+++ b/libio/vstpeprintf.c
@@ -0,0 +1,52 @@
+/* Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library 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.
+
+   The GNU C Library 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 the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.
+
+   As a special exception, if you link the code in this file with
+   files compiled with a GNU compiler to produce an executable,
+   that does not cause the resulting executable to be covered by
+   the GNU Lesser General Public License.  This exception does not
+   however invalidate any other reasons why the executable file
+   might be covered by the GNU Lesser General Public License.
+   This exception applies to code released by its copyright holders
+   in files containing the exception.  */
+
+#include <stdarg.h>
+#include <libioP.h>
+
+
+char *
+vstpeprintf(char *dst, char *end, const char *restrict fmt, va_list ap)
+{
+	int  dsize, len;
+
+	if (dst == end)
+		return end;
+	if (dst == NULL)
+		return NULL;
+	if (dst > end)
+		__builtin_unreachable();
+
+	dsize = end - dst;
+	len = __vsnprintf_internal(dst, dsize, fmt, ap, 0);
+
+	if (len == -1)
+		return NULL;
+	if (len >= dsize)
+		return end;
+
+	return dst + len;
+}
-- 
2.39.0



More information about the Libc-alpha mailing list