Skip to content

Commit 423fd65

Browse files
alejandro-colomarhallyn
authored andcommitted
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 <[email protected]>
1 parent d895ee1 commit 423fd65

File tree

6 files changed

+95
-111
lines changed

6 files changed

+95
-111
lines changed

lib/string/sprintf/xaprintf.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
5+
#include <config.h>
6+
7+
#include "string/sprintf/xaprintf.h"
8+
9+
#include <stdarg.h>
10+
11+
12+
extern inline char *xaprintf(const char *restrict fmt, ...);
13+
extern inline char *xvaprintf(const char *restrict fmt, va_list ap);

lib/string/sprintf/xaprintf.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <[email protected]>
2+
// SPDX-License-Identifier: BSD-3-Clause
3+
4+
5+
#ifndef SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
6+
#define SHADOW_INCLUDE_LIB_STRING_SPRINTF_XASPRINTF_H_
7+
8+
9+
#include <config.h>
10+
11+
#include <stdarg.h>
12+
#include <stddef.h>
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
16+
#include "attr.h"
17+
#include "string/sprintf/aprintf.h"
18+
19+
20+
ATTR_MALLOC(free)
21+
format_attr(printf, 1, 2)
22+
inline char *xaprintf(const char *restrict fmt, ...);
23+
24+
ATTR_MALLOC(free)
25+
format_attr(printf, 1, 0)
26+
inline char *xvaprintf(const char *restrict fmt, va_list ap);
27+
28+
29+
// exit-on-error allocate print formatted
30+
inline char *
31+
xaprintf(const char *restrict fmt, ...)
32+
{
33+
char *p;
34+
va_list ap;
35+
36+
va_start(ap, fmt);
37+
p = xvaprintf(fmt, ap);
38+
va_end(ap);
39+
40+
return p;
41+
}
42+
43+
44+
inline char *
45+
xvaprintf(const char *restrict fmt, va_list ap)
46+
{
47+
char *p;
48+
49+
p = vaprintf(fmt, ap);
50+
if (p == NULL) {
51+
perror("vaprintf");
52+
exit(EXIT_FAILURE);
53+
}
54+
55+
return p;
56+
}
57+
58+
59+
#endif // include guard

lib/string/sprintf/xasprintf.c

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/string/sprintf/xasprintf.h

Lines changed: 0 additions & 54 deletions
This file was deleted.

tests/unit/Makefile.am

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ check_PROGRAMS = \
1111
test_strncpy \
1212
test_strtcpy \
1313
test_typetraits \
14-
test_xasprintf
14+
test_xaprintf
1515

1616
if ENABLE_LOGIND
1717
check_PROGRAMS += \
@@ -129,18 +129,19 @@ test_typetraits_LDADD = \
129129
$(CMOCKA_LIBS) \
130130
$(NULL)
131131

132-
test_xasprintf_SOURCES = \
133-
../../lib/string/sprintf/xasprintf.c \
134-
test_xasprintf.c \
132+
test_xaprintf_SOURCES = \
133+
../../lib/string/sprintf/aprintf.c \
134+
../../lib/string/sprintf/xaprintf.c \
135+
test_xaprintf.c \
135136
$(NULL)
136-
test_xasprintf_CFLAGS = \
137+
test_xaprintf_CFLAGS = \
137138
$(AM_CFLAGS) \
138139
$(NULL)
139-
test_xasprintf_LDFLAGS = \
140+
test_xaprintf_LDFLAGS = \
140141
-Wl,-wrap,vasprintf \
141142
-Wl,-wrap,exit \
142143
$(NULL)
143-
test_xasprintf_LDADD = \
144+
test_xaprintf_LDADD = \
144145
$(CMOCKA_LIBS) \
145146
$(NULL)
146147

tests/unit/test_xasprintf.c renamed to tests/unit/test_xaprintf.c

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
#include <stdint.h> // Required by <cmocka.h>
1515
#include <cmocka.h>
1616

17-
#include "string/sprintf/xasprintf.h"
17+
#include "string/sprintf/xaprintf.h"
1818

1919

2020
#define smock() _Generic(mock(), uintmax_t: (intmax_t) mock())
2121
#define assert_unreachable() assert_true(0)
2222

23-
#define XASPRINTF_CALLED (-36)
2423
#define EXIT_CALLED (42)
25-
#define TEST_OK (-6)
2624

2725

2826
static jmp_buf jmpb;
@@ -32,20 +30,16 @@ int __real_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
3230
int __wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap);
3331
void __wrap_exit(int status);
3432

35-
[[gnu::noipa]]
36-
static int xasprintf_volatile(char *volatile *restrict s,
37-
const char *restrict fmt, ...);
38-
39-
static void test_xasprintf_exit(void **state);
40-
static void test_xasprintf_ok(void **state);
33+
static void test_xaprintf_exit(void **state);
34+
static void test_xaprintf_ok(void **state);
4135

4236

4337
int
4438
main(void)
4539
{
4640
const struct CMUnitTest tests[] = {
47-
cmocka_unit_test(test_xasprintf_exit),
48-
cmocka_unit_test(test_xasprintf_ok),
41+
cmocka_unit_test(test_xaprintf_exit),
42+
cmocka_unit_test(test_xaprintf_ok),
4943
};
5044

5145
return cmocka_run_group_tests(tests, NULL, NULL);
@@ -66,58 +60,43 @@ __wrap_exit(int status)
6660
}
6761

6862

69-
static int
70-
xasprintf_volatile(char *volatile *restrict s, const char *restrict fmt, ...)
71-
{
72-
int len;
73-
va_list ap;
74-
75-
va_start(ap, fmt);
76-
len = xvasprintf((char **) s, fmt, ap);
77-
va_end(ap);
78-
}
79-
80-
8163
static void
82-
test_xasprintf_exit(void **state)
64+
test_xaprintf_exit(void **state)
8365
{
84-
volatile int len;
8566
char *volatile p;
8667

8768
will_return(__wrap_vasprintf, -1);
8869

89-
len = 0;
70+
p = "before";
9071

9172
switch (setjmp(jmpb)) {
9273
case 0:
93-
len = XASPRINTF_CALLED;
94-
len = xasprintf_volatile(&p, "foo%s", "bar");
74+
p = "xaprintf_called";
75+
p = xaprintf("foo%s", "bar");
9576
assert_unreachable();
9677
break;
9778
case EXIT_CALLED:
98-
assert_int_equal(len, XASPRINTF_CALLED);
99-
len = TEST_OK;
79+
assert_true(strcmp(p, "xaprintf_called"));
80+
p = "test_ok";
10081
break;
10182
default:
10283
assert_unreachable();
10384
break;
10485
}
10586

106-
assert_int_equal(len, TEST_OK);
87+
assert_true(strcmp(p, "test_ok"));
10788
}
10889

10990

11091
static void
111-
test_xasprintf_ok(void **state)
92+
test_xaprintf_ok(void **state)
11293
{
113-
int len;
11494
char *p;
11595

116-
// Trick: it will actually return the length, not 0.
96+
// Trick: vasprintf(3) will actually return the new string, not 0.
11797
will_return(__wrap_vasprintf, 0);
11898

119-
len = xasprintf(&p, "foo%d%s", 1, "bar");
120-
assert_int_equal(len, strlen("foo1bar"));
99+
p = xaprintf("foo%d%s", 1, "bar");
121100
assert_string_equal(p, "foo1bar");
122101
free(p);
123102
}

0 commit comments

Comments
 (0)