From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [185.226.149.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 90BE239B96F for ; Mon, 2 Mar 2026 10:18:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.37 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446738; cv=none; b=dXw343YXU0fFWrQNbK7DgTFrLt/jpGHSecutO31MiDSqSE2dTEiQkEibW2oXF2dKGBgVcP8W66Au/T68132cAnT52D59CVonOuRs0ARWv/M/7kUh6T7/f3RBP6J/fkB1z2L313c7pevhSikbhHKn/fQGv6EoWRbEjbbF2ambV/w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446738; c=relaxed/simple; bh=5BsBiOG7tF0nIGMZ7N95BCa5igR/H8JnvlprjuIjToU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dlP90q7797EbOtuXQgoksTZCgDvqkucfP/HlUC815BJV2CEPaqmyLRCka8HRUysgBntDEMW/qcAfXUXsXomOdce/UJdfEV2xTVz5vSPjTGqdnzxWKllED9r2YQ2gmOH6J8Rfq3qHVK7NVPFMi7ma9OaS4jeX+5e+/RoCo46/isM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=kg9GxFJP; arc=none smtp.client-ip=185.226.149.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="kg9GxFJP" Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vx0MX-003850-Hi; Mon, 02 Mar 2026 11:18:53 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=zaRST1Uuvh431GacKGwhMaa1JzSEfqW4nhRFu8bcjaE=; b=kg9GxFJPTTVMK9JUpfeeVE37v/ DEBC169WsR9bHt3RVo7Pe5IdjOX6Wp/x5luZxnrfG0izl5sK0aWwN4cDnYRgIDWeD2Uzep8BzPLDV 6zQTyv4ldS6pO+8L/+cxTXmOfKiXZz+lsLyAdlKq2F2se4H6H9Z7lKvfWl8R/G6OAEUR3JiyVxzis 520mRCM/C0oFPP8bjgInaFzmFhE66Qtlj2ct6HXeJU0Y8vkYem4GVE4ScUZwgZxTexUqYtFS3tAvZ b7LekpMsxbLxRR2i8Y/aka6/8fBhcvO0GI6Nm+3XIIH++Di/6OJHkgeljn+n5QFzngKpKwUMC5mSR lVuCoKsw==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vx0MX-00055u-8V; Mon, 02 Mar 2026 11:18:53 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vx0M9-006y7o-ML; Mon, 02 Mar 2026 11:18:29 +0100 From: david.laight.linux@gmail.com To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Cheng Li Cc: David Laight Subject: [PATCH v4 next 05/23] selftests/nolibc: Use length of 'expected' string to check snprintf() output Date: Mon, 2 Mar 2026 10:17:57 +0000 Message-Id: <20260302101815.3043-6-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260302101815.3043-1-david.laight.linux@gmail.com> References: <20260302101815.3043-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: David Laight 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 --- v4: split out from patch 3. tools/testing/selftests/nolibc/nolibc-test.c | 67 ++++++++++++-------- 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index f4813764d56e..dc60ac0d1a05 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1660,29 +1660,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; } @@ -1809,21 +1824,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; -- 2.39.5