selftests/nolibc: Use length of 'expected' string to check snprintf() output

Instead of requiring the test cases specifying both the length and
expected output, take the length from the expected output.
Tests that expect the output be truncated are changed to specify
the un-truncated output.

Change the strncmp() to a memcmp() with an extra check that the
output is actually terminated.

Append a '+' to the printed output (after the final ") when the output
is truncated.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260302101815.3043-6-david.laight.linux@gmail.com
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
This commit is contained in:
David Laight 2026-03-02 10:17:57 +00:00 committed by Thomas Weißschuh
parent 9aa8a4afd4
commit f36e1ec61a

View File

@ -1668,29 +1668,44 @@ int run_stdlib(int min, int max)
return ret;
}
#define EXPECT_VFPRINTF(c, expected, fmt, ...) \
ret += expect_vfprintf(llen, c, expected, fmt, ##__VA_ARGS__)
#define EXPECT_VFPRINTF(expected, fmt, ...) \
ret += expect_vfprintf(llen, expected, fmt, ##__VA_ARGS__)
static int expect_vfprintf(int llen, int c, const char *expected, const char *fmt, ...)
#define VFPRINTF_LEN 20
static int expect_vfprintf(int llen, const char *expected, const char *fmt, ...)
{
char buf[100];
char buf[VFPRINTF_LEN + 80];
unsigned int cmp_len;
va_list args;
ssize_t w;
ssize_t w, expected_len;
va_start(args, fmt);
/* Only allow writing 21 bytes, to test truncation */
w = vsnprintf(buf, 21, fmt, args);
/* Limit buffer length to test truncation */
w = vsnprintf(buf, VFPRINTF_LEN + 1, fmt, args);
va_end(args);
llen += printf(" \"%s\"", buf);
if (strncmp(expected, buf, c)) {
llen += printf(" should be \"%s\"", expected);
expected_len = strlen(expected);
if (expected_len > VFPRINTF_LEN) {
/* Indicate truncated in test output */
llen += printf("+");
cmp_len = VFPRINTF_LEN;
} else {
cmp_len = expected_len;
}
if (memcmp(expected, buf, cmp_len) || buf[cmp_len]) {
/* Copy and truncate until "%.*s" supported */
memcpy(buf, expected, cmp_len);
buf[cmp_len] = 0;
llen += printf(" should be \"%s\"", buf);
result(llen, FAIL);
return 1;
}
if (w != c) {
llen += printf(" written(%d) != %d", (int)w, c);
if (w != expected_len) {
llen += printf(" written(%d) != %d", (int)w, (int)expected_len);
result(llen, FAIL);
return 1;
}
@ -1817,21 +1832,21 @@ static int run_printf(int min, int max)
* test numbers.
*/
switch (test + __LINE__ + 1) {
CASE_TEST(empty); EXPECT_VFPRINTF(0, "", ""); break;
CASE_TEST(simple); EXPECT_VFPRINTF(3, "foo", "foo"); break;
CASE_TEST(string); EXPECT_VFPRINTF(3, "foo", "%s", "foo"); break;
CASE_TEST(number); EXPECT_VFPRINTF(4, "1234", "%d", 1234); break;
CASE_TEST(negnumber); EXPECT_VFPRINTF(5, "-1234", "%d", -1234); break;
CASE_TEST(unsigned); EXPECT_VFPRINTF(5, "12345", "%u", 12345); break;
CASE_TEST(char); EXPECT_VFPRINTF(1, "c", "%c", 'c'); break;
CASE_TEST(hex); EXPECT_VFPRINTF(1, "f", "%x", 0xf); break;
CASE_TEST(pointer); EXPECT_VFPRINTF(3, "0x1", "%p", (void *) 0x1); break;
CASE_TEST(uintmax_t); EXPECT_VFPRINTF(20, "18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
CASE_TEST(intmax_t); EXPECT_VFPRINTF(20, "-9223372036854775807", "%jd", 0x8000000000000001LL); break;
CASE_TEST(truncation); EXPECT_VFPRINTF(25, "01234567890123456789", "%s", "0123456789012345678901234"); break;
CASE_TEST(string_width); EXPECT_VFPRINTF(10, " 1", "%10s", "1"); break;
CASE_TEST(number_width); EXPECT_VFPRINTF(10, " 1", "%10d", 1); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(25, " ", "%25d", 1); break;
CASE_TEST(empty); EXPECT_VFPRINTF("", ""); break;
CASE_TEST(simple); EXPECT_VFPRINTF("foo", "foo"); break;
CASE_TEST(string); EXPECT_VFPRINTF("foo", "%s", "foo"); break;
CASE_TEST(number); EXPECT_VFPRINTF("1234", "%d", 1234); break;
CASE_TEST(negnumber); EXPECT_VFPRINTF("-1234", "%d", -1234); break;
CASE_TEST(unsigned); EXPECT_VFPRINTF("12345", "%u", 12345); break;
CASE_TEST(char); EXPECT_VFPRINTF("c", "%c", 'c'); break;
CASE_TEST(hex); EXPECT_VFPRINTF("f", "%x", 0xf); break;
CASE_TEST(pointer); EXPECT_VFPRINTF("0x1", "%p", (void *) 0x1); break;
CASE_TEST(uintmax_t); EXPECT_VFPRINTF("18446744073709551615", "%ju", 0xffffffffffffffffULL); break;
CASE_TEST(intmax_t); EXPECT_VFPRINTF("-9223372036854775807", "%jd", 0x8000000000000001LL); break;
CASE_TEST(truncation); EXPECT_VFPRINTF("0123456789012345678901234", "%s", "0123456789012345678901234"); break;
CASE_TEST(string_width); EXPECT_VFPRINTF(" 1", "%10s", "1"); break;
CASE_TEST(number_width); EXPECT_VFPRINTF(" 1", "%10d", 1); break;
CASE_TEST(width_trunc); EXPECT_VFPRINTF(" 1", "%25d", 1); break;
CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break;
CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break;
CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;