]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf()
authorAlejandro Colomar <alx@kernel.org>
Wed, 1 Jan 2025 13:07:40 +0000 (14:07 +0100)
committerSerge Hallyn <serge@hallyn.com>
Wed, 4 Jun 2025 04:25:02 +0000 (23:25 -0500)
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>
lib/string/sprintf/xaprintf.c [new file with mode: 0644]
lib/string/sprintf/xaprintf.h [new file with mode: 0644]
lib/string/sprintf/xasprintf.c [deleted file]
lib/string/sprintf/xasprintf.h [deleted file]
tests/unit/Makefile.am
tests/unit/test_xaprintf.c [moved from tests/unit/test_xasprintf.c with 59% similarity]

diff --git a/lib/string/sprintf/xaprintf.c b/lib/string/sprintf/xaprintf.c
new file mode 100644 (file)
index 0000000..0288065
--- /dev/null
@@ -0,0 +1,13 @@
+// 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);
diff --git a/lib/string/sprintf/xaprintf.h b/lib/string/sprintf/xaprintf.h
new file mode 100644 (file)
index 0000000..b6c10ef
--- /dev/null
@@ -0,0 +1,59 @@
+// 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
diff --git a/lib/string/sprintf/xasprintf.c b/lib/string/sprintf/xasprintf.c
deleted file mode 100644 (file)
index 2c74056..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-// 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);
diff --git a/lib/string/sprintf/xasprintf.h b/lib/string/sprintf/xasprintf.h
deleted file mode 100644 (file)
index 073cb90..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-// 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
index 4dff894ace522db671bd909e36eb2f38b965810f..6e943183b8ccf23b2f483b2a7a315ee0d9b6df39 100644 (file)
@@ -11,7 +11,7 @@ check_PROGRAMS = \
     test_strncpy \
     test_strtcpy \
     test_typetraits \
-    test_xasprintf
+    test_xaprintf
 
 if ENABLE_LOGIND
 check_PROGRAMS += \
@@ -129,18 +129,19 @@ test_typetraits_LDADD = \
     $(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)
 
similarity index 59%
rename from tests/unit/test_xasprintf.c
rename to tests/unit/test_xaprintf.c
index b28470f62fcc70116a5db803a5d14bae108209a4..156528a1763f7721f44cd96ae05b3eb048cdd518 100644 (file)
 #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;
@@ -32,20 +30,16 @@ int __real_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
 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);
@@ -66,58 +60,43 @@ __wrap_exit(int status)
 }
 
 
-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);
 }