From: Alejandro Colomar Date: Wed, 1 Jan 2025 12:10:35 +0000 (+0100) Subject: lib/string/sprintf/: [v]aprintf(): Add functions X-Git-Tag: 4.18.0-rc1~25 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5949132308ca976fd51fbade778564b0f15644bb;p=thirdparty%2Fshadow.git lib/string/sprintf/: [v]aprintf(): Add functions These functions are just like [v]asprintf(3), but simpler. They return the newly allocated memory, which allows us to use the [[gnu::malloc(free)]] attribute, which enhances static analysis. They also omit the length, which we don't care about at all. As a curiosity, Plan9 seems to provide this same API, under the name smprint(3). Link: Signed-off-by: Alejandro Colomar --- diff --git a/lib/Makefile.am b/lib/Makefile.am index d9641b946..621ac29e3 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -193,6 +193,8 @@ libshadow_la_SOURCES = \ string/ctype/strtoascii/strtolower.h \ string/memset/memzero.c \ string/memset/memzero.h \ + string/sprintf/aprintf.c \ + string/sprintf/aprintf.h \ string/sprintf/snprintf.c \ string/sprintf/snprintf.h \ string/sprintf/stpeprintf.c \ diff --git a/lib/string/sprintf/aprintf.c b/lib/string/sprintf/aprintf.c new file mode 100644 index 000000000..d243d9c45 --- /dev/null +++ b/lib/string/sprintf/aprintf.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/sprintf/aprintf.h" + +#include + + +extern inline char *aprintf(const char *restrict fmt, ...); +extern inline char *vaprintf(const char *restrict fmt, va_list ap); diff --git a/lib/string/sprintf/aprintf.h b/lib/string/sprintf/aprintf.h new file mode 100644 index 000000000..f4cd7217b --- /dev/null +++ b/lib/string/sprintf/aprintf.h @@ -0,0 +1,57 @@ +// SPDX-FileCopyrightText: 2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_ +#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_ + + +#include + +#include +#include +#include +#include + +#include "attr.h" + + +ATTR_MALLOC(free) +format_attr(printf, 1, 2) +inline char *aprintf(const char *restrict fmt, ...); + +ATTR_MALLOC(free) +format_attr(printf, 1, 0) +inline char *vaprintf(const char *restrict fmt, va_list ap); + + +// allocate print formatted +// Like asprintf(3), but simpler; omit the length. +inline char * +aprintf(const char *restrict fmt, ...) +{ + char *p; + va_list ap; + + va_start(ap, fmt); + p = vaprintf(fmt, ap); + va_end(ap); + + return p; +} + + +// Like vasprintf(3), but simpler; omit the length. +inline char * +vaprintf(const char *restrict fmt, va_list ap) +{ + char *p; + + if (vasprintf(&p, fmt, ap) == -1) + return NULL; + + return p; +} + + +#endif // include guard