Wrap [v]aprintf() instead of [v]asprintf(3).
Repurpose x[v]asprintf()'s tests to test x[v]aprintf().
Signed-off-by: Alejandro Colomar <alx@kernel.org>
--- /dev/null
+// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#include <config.h>
+
+#include "string/sprintf/xaprintf.h"
+
+#include <stdarg.h>
+
+
+extern inline char *xaprintf(const char *restrict fmt, ...);
+extern inline char *xvaprintf(const char *restrict fmt, va_list ap);
--- /dev/null
+// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: BSD-3-Clause
+
+
+#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
+#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
+
+
+#include <config.h>
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "attr.h"
+#include "string/sprintf/aprintf.h"
+
+
+ATTR_MALLOC(free)
+format_attr(printf, 1, 2)
+inline char *xaprintf(const char *restrict fmt, ...);
+
+ATTR_MALLOC(free)
+format_attr(printf, 1, 0)
+inline char *xvaprintf(const char *restrict fmt, va_list ap);
+
+
+// exit-on-error allocate print formatted
+inline char *
+xaprintf(const char *restrict fmt, ...)
+{
+ char *p;
+ va_list ap;
+
+ va_start(ap, fmt);
+ p = xvaprintf(fmt, ap);
+ va_end(ap);
+
+ return p;
+}
+
+
+inline char *
+xvaprintf(const char *restrict fmt, va_list ap)
+{
+ char *p;
+
+ p = vaprintf(fmt, ap);
+ if (p == NULL) {
+ perror("vaprintf");
+ exit(EXIT_FAILURE);
+ }
+
+ return p;
+}
+
+
+#endif // include guard
+++ /dev/null
-// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
-// SPDX-License-Identifier: BSD-3-Clause
-
-
-#include <config.h>
-
-#include "string/sprintf/xasprintf.h"
-
-#include <stdarg.h>
-
-
-extern inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
-extern inline int xvasprintf(char **restrict s, const char *restrict fmt,
- va_list ap);
+++ /dev/null
-// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
-// SPDX-License-Identifier: BSD-3-Clause
-
-
-#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
-#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
-
-
-#include <config.h>
-
-#include <stdarg.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "attr.h"
-
-
-format_attr(printf, 2, 3)
-inline int xasprintf(char **restrict s, const char *restrict fmt, ...);
-format_attr(printf, 2, 0)
-inline int xvasprintf(char **restrict s, const char *restrict fmt, va_list ap);
-
-
-inline int
-xasprintf(char **restrict s, const char *restrict fmt, ...)
-{
- int len;
- va_list ap;
-
- va_start(ap, fmt);
- len = xvasprintf(s, fmt, ap);
- va_end(ap);
-
- return len;
-}
-
-
-inline int
-xvasprintf(char **restrict s, const char *restrict fmt, va_list ap)
-{
- int len;
-
- len = vasprintf(s, fmt, ap);
- if (len == -1) {
- perror("asprintf");
- exit(EXIT_FAILURE);
- }
-
- return len;
-}
-
-
-#endif // include guard
test_strncpy \
test_strtcpy \
test_typetraits \
- test_xasprintf
+ test_xaprintf
if ENABLE_LOGIND
check_PROGRAMS += \
$(CMOCKA_LIBS) \
$(NULL)
-test_xasprintf_SOURCES = \
- ../../lib/string/sprintf/xasprintf.c \
- test_xasprintf.c \
+test_xaprintf_SOURCES = \
+ ../../lib/string/sprintf/aprintf.c \
+ ../../lib/string/sprintf/xaprintf.c \
+ test_xaprintf.c \
$(NULL)
-test_xasprintf_CFLAGS = \
+test_xaprintf_CFLAGS = \
$(AM_CFLAGS) \
$(NULL)
-test_xasprintf_LDFLAGS = \
+test_xaprintf_LDFLAGS = \
-Wl,-wrap,vasprintf \
-Wl,-wrap,exit \
$(NULL)
-test_xasprintf_LDADD = \
+test_xaprintf_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)
#include <stdint.h> // Required by <cmocka.h>
#include <cmocka.h>
-#include "string/sprintf/xasprintf.h"
+#include "string/sprintf/xaprintf.h"
#define smock() _Generic(mock(), uintmax_t: (intmax_t) mock())
#define assert_unreachable() assert_true(0)
-#define XASPRINTF_CALLED (-36)
#define EXIT_CALLED (42)
-#define TEST_OK (-6)
static jmp_buf jmpb;
int __wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
void __wrap_exit(int status);
-[[gnu::noipa]]
-static int xasprintf_volatile(char *volatile *restrict s,
- const char *restrict fmt, ...);
-
-static void test_xasprintf_exit(void **state);
-static void test_xasprintf_ok(void **state);
+static void test_xaprintf_exit(void **state);
+static void test_xaprintf_ok(void **state);
int
main(void)
{
const struct CMUnitTest tests[] = {
- cmocka_unit_test(test_xasprintf_exit),
- cmocka_unit_test(test_xasprintf_ok),
+ cmocka_unit_test(test_xaprintf_exit),
+ cmocka_unit_test(test_xaprintf_ok),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
-static int
-xasprintf_volatile(char *volatile *restrict s, const char *restrict fmt, ...)
-{
- int len;
- va_list ap;
-
- va_start(ap, fmt);
- len = xvasprintf((char **) s, fmt, ap);
- va_end(ap);
-}
-
-
static void
-test_xasprintf_exit(void **state)
+test_xaprintf_exit(void **state)
{
- volatile int len;
char *volatile p;
will_return(__wrap_vasprintf, -1);
- len = 0;
+ p = "before";
switch (setjmp(jmpb)) {
case 0:
- len = XASPRINTF_CALLED;
- len = xasprintf_volatile(&p, "foo%s", "bar");
+ p = "xaprintf_called";
+ p = xaprintf("foo%s", "bar");
assert_unreachable();
break;
case EXIT_CALLED:
- assert_int_equal(len, XASPRINTF_CALLED);
- len = TEST_OK;
+ assert_true(strcmp(p, "xaprintf_called"));
+ p = "test_ok";
break;
default:
assert_unreachable();
break;
}
- assert_int_equal(len, TEST_OK);
+ assert_true(strcmp(p, "test_ok"));
}
static void
-test_xasprintf_ok(void **state)
+test_xaprintf_ok(void **state)
{
- int len;
char *p;
- // Trick: it will actually return the length, not 0.
+ // Trick: vasprintf(3) will actually return the new string, not 0.
will_return(__wrap_vasprintf, 0);
- len = xasprintf(&p, "foo%d%s", 1, "bar");
- assert_int_equal(len, strlen("foo1bar"));
+ p = xaprintf("foo%d%s", 1, "bar");
assert_string_equal(p, "foo1bar");
free(p);
}