mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shuvam Pandey <shuvampandey1@gmail.com>
To: Kees Cook <kees@kernel.org>, Andy Shevchenko <andy@kernel.org>
Cc: James Bottomley <James.Bottomley@HansenPartnership.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] lib/tests: add string_get_size() boundary tests
Date: Wed, 22 Apr 2026 02:04:06 +0545	[thread overview]
Message-ID: <177680283372.84187.11707577520741615468@gmail.com> (raw)

string_get_size() has several non-obvious outputs around decimal and
binary unit boundaries because the unit is chosen before the final
rounding step. Decimal values just below 1 MB can print as "1000 kB";
binary values below 1 KiB stay in bytes; and rounded binary values can
print as "1000 KiB" or "1024 KiB" before the input reaches the next
binary unit.

Add KUnit coverage for these current outputs and nearby threshold values
so the existing behavior is explicit.

Link: https://lore.kernel.org/r/CAHp75VfB0Y7K=chk5wpKtZcgVfCN5X7zhNEq6QSrirdgHs5_bQ@mail.gmail.com
Link: https://lore.kernel.org/r/696d25567aadbb8ba324d4a980c15eb5cf4ccef6.camel@HansenPartnership.com
Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
---
 lib/tests/string_helpers_kunit.c | 33 ++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_kunit.c
index c853046183d24..92454837f3ddd 100644
--- a/lib/tests/string_helpers_kunit.c
+++ b/lib/tests/string_helpers_kunit.c
@@ -558,6 +558,39 @@ static void test_get_size(struct kunit *test)
 	/* weird block sizes */
 	test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
 
+	/* decimal boundary around 1 MB */
+	test_string_get_size_one(999499, 1, "999 kB", "976 KiB");
+	test_string_get_size_one(999500, 1, "1000 kB", "976 KiB");
+	test_string_get_size_one(999999, 1, "1000 kB", "977 KiB");
+	test_string_get_size_one(1000000, 1, "1.00 MB", "977 KiB");
+	test_string_get_size_one(1000001, 1, "1.00 MB", "977 KiB");
+
+	/* binary values stay in bytes until the 1 KiB boundary */
+	test_string_get_size_one(1000, 1, "1.00 kB", "1000 B");
+	test_string_get_size_one(1018, 1, "1.02 kB", "1018 B");
+	test_string_get_size_one(1023, 1, "1.02 kB", "1023 B");
+	test_string_get_size_one(1024, 1, "1.02 kB", "1.00 KiB");
+	test_string_get_size_one(1025, 1, "1.03 kB", "1.00 KiB");
+
+	/* binary rounding around 1000 KiB */
+	test_string_get_size_one(1023487, 1, "1.02 MB", "999 KiB");
+	test_string_get_size_one(1023488, 1, "1.02 MB", "1000 KiB");
+	test_string_get_size_one(1024000, 1, "1.02 MB", "1000 KiB");
+	test_string_get_size_one(1024511, 1, "1.02 MB", "1000 KiB");
+	test_string_get_size_one(1024512, 1, "1.02 MB", "1001 KiB");
+
+	/* binary boundary around 1 MiB */
+	test_string_get_size_one(1048063, 1, "1.05 MB", "1023 KiB");
+	test_string_get_size_one(1048064, 1, "1.05 MB", "1024 KiB");
+	test_string_get_size_one(1048575, 1, "1.05 MB", "1024 KiB");
+	test_string_get_size_one(1048576, 1, "1.05 MB", "1.00 MiB");
+	test_string_get_size_one(1048577, 1, "1.05 MB", "1.00 MiB");
+
+	/* binary boundary around 2 MiB */
+	test_string_get_size_one(2097151, 1, "2.10 MB", "2.00 MiB");
+	test_string_get_size_one(2097152, 1, "2.10 MB", "2.00 MiB");
+	test_string_get_size_one(2097153, 1, "2.10 MB", "2.00 MiB");
+
 	/* huge values */
 	test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
 	test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
-- 
2.50.1 (Apple Git-155)



             reply	other threads:[~2026-04-21 20:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-21 20:19 Shuvam Pandey [this message]
2026-04-21 20:32 ` Josh Law
2026-04-22  7:32 ` Andy Shevchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=177680283372.84187.11707577520741615468@gmail.com \
    --to=shuvampandey1@gmail.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®