Add stpeprintf()

[v]stpeprintf() are similar to [v]snprintf(3), but they allow chaining.
[v]snprintf(3) are very dangerous for catenating strings, since the
obvious ways to do it invoke Undefined Behavior, and the ways that avoid
UB are very error-prone.

Cc: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-01-30 00:19:56 +01:00
committed by Iker Pedrosa
parent a187ad8e9e
commit 7e213cfb50
4 changed files with 146 additions and 1 deletions

25
libmisc/stpeprintf.c Normal file
View File

@ -0,0 +1,25 @@
/*
* SPDX-FileCopyrightText: 2022 - 2023, Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <config.h>
#if !defined(HAVE_STPEPRINTF)
#ident "$Id$"
#include "stpeprintf.h"
#include <stdarg.h>
extern inline char *stpeprintf(char *dst, char *end, const char *restrict fmt,
...);
extern inline char *vstpeprintf(char *dst, char *end, const char *restrict fmt,
va_list ap);
#endif // !HAVE_STPEPRINTF