mirror of
https://github.com/shadow-maint/shadow.git
synced 2026-01-26 14:03:17 +00:00
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 <alx@kernel.org>
This commit is contained in:
parent
d895ee1ac8
commit
423fd652b5
13
lib/string/sprintf/xaprintf.c
Normal file
13
lib/string/sprintf/xaprintf.c
Normal file
@ -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);
|
||||
59
lib/string/sprintf/xaprintf.h
Normal file
59
lib/string/sprintf/xaprintf.h
Normal file
@ -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
|
||||
@ -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);
|
||||
@ -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
|
||||
@ -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)
|
||||
|
||||
|
||||
@ -14,15 +14,13 @@
|
||||
#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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user