]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/sprintf/: [v]aprintf(): Add functions
authorAlejandro Colomar <alx@kernel.org>
Wed, 1 Jan 2025 12:10:35 +0000 (13:10 +0100)
committerSerge Hallyn <serge@hallyn.com>
Wed, 4 Jun 2025 04:25:02 +0000 (23:25 -0500)
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: <https://9fans.github.io/plan9port/man/man3/print.html>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/Makefile.am
lib/string/sprintf/aprintf.c [new file with mode: 0644]
lib/string/sprintf/aprintf.h [new file with mode: 0644]

index d9641b94607dd608941cb6150e79f80ef9999e74..621ac29e3cbae8b86075585320f13b501ad7d7e8 100644 (file)
@@ -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 (file)
index 0000000..d243d9c
--- /dev/null
@@ -0,0 +1,13 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/sprintf/aprintf.h"
+
+#include <stdarg.h>
+
+
+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 (file)
index 0000000..f4cd721
--- /dev/null
@@ -0,0 +1,57 @@
+// SPDX-FileCopyrightText: 2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_
+#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_APRINTF_H_
+
+
+#include <config.h>
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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