From: Alejandro Colomar Date: Wed, 1 Jan 2025 13:07:40 +0000 (+0100) Subject: lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf() X-Git-Tag: 4.18.0-rc1~21 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=423fd652b5636d928f2cabbc4680867516b3ba90;p=thirdparty%2Fshadow.git lib/string/sprintf/, tests/unit/: Transform x[v]asprintf() into x[v]aprintf() Wrap [v]aprintf() instead of [v]asprintf(3). Repurpose x[v]asprintf()'s tests to test x[v]aprintf(). Signed-off-by: Alejandro Colomar --- diff --git a/lib/string/sprintf/xaprintf.c b/lib/string/sprintf/xaprintf.c new file mode 100644 index 000000000..028806561 --- /dev/null +++ b/lib/string/sprintf/xaprintf.c @@ -0,0 +1,13 @@ +// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include "string/sprintf/xaprintf.h" + +#include + + +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 index 000000000..b6c10ef93 --- /dev/null +++ b/lib/string/sprintf/xaprintf.h @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_ +#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_ + + +#include + +#include +#include +#include +#include + +#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 index 2c7405651..000000000 --- a/lib/string/sprintf/xasprintf.c +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#include - -#include "string/sprintf/xasprintf.h" - -#include - - -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 index 073cb9031..000000000 --- a/lib/string/sprintf/xasprintf.h +++ /dev/null @@ -1,54 +0,0 @@ -// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar -// SPDX-License-Identifier: BSD-3-Clause - - -#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_ -#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_ - - -#include - -#include -#include -#include -#include - -#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 diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 4dff894ac..6e943183b 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -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) diff --git a/tests/unit/test_xasprintf.c b/tests/unit/test_xaprintf.c similarity index 59% rename from tests/unit/test_xasprintf.c rename to tests/unit/test_xaprintf.c index b28470f62..156528a17 100644 --- a/tests/unit/test_xasprintf.c +++ b/tests/unit/test_xaprintf.c @@ -14,15 +14,13 @@ #include // Required by #include -#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); }