public inbox for linux-hardening@vger.kernel.org
 help / color / mirror / Atom feed
From: Shuvam Pandey <shuvampandey1@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Kees Cook <kees@kernel.org>, Andy Shevchenko <andy@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: [PATCH] lib/string_helpers: fix string_get_size() unit promotion after rounding
Date: Tue,  7 Apr 2026 20:18:06 +0545	[thread overview]
Message-ID: <20260407143306.78809-1-shuvampandey1@gmail.com> (raw)

string_get_size() rounds the fractional part before formatting the
result. If that carry pushes the integer part to the unit divisor, the
value is still printed in the old unit.

This yields outputs like "1000 kB" for 999500 bytes and "1024 KiB" for
1048064 bytes instead of promoting them to the next unit.

Renormalize the value after the carry so it is promoted before
formatting. Add KUnit coverage for the decimal and binary boundary
cases.

Fixes: 3c9f3681d0b4 ("[SCSI] lib: add generic helper to print sizes rounded to the correct SI range")
Signed-off-by: Shuvam Pandey <shuvampandey1@gmail.com>
---
 lib/string_helpers.c             | 7 +++++++
 lib/tests/string_helpers_kunit.c | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 169eaf583494..aa1ba47e28bd 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -121,6 +121,13 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
 		size += 1;
 	}
 
+	if (size >= divisor[units_base]) {
+		size = 1;
+		remainder = 0;
+		i++;
+		j = 2;
+	}
+
 	if (j) {
 		snprintf(tmp, sizeof(tmp), ".%03u", remainder);
 		tmp[j+1] = '\0';
diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_kunit.c
index c853046183d2..16118fb16c9a 100644
--- a/lib/tests/string_helpers_kunit.c
+++ b/lib/tests/string_helpers_kunit.c
@@ -558,6 +558,10 @@ static void test_get_size(struct kunit *test)
 	/* weird block sizes */
 	test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
 
+	/* rounding carry into the next unit */
+	test_string_get_size_one(999500, 1, "1.00 MB", "976 KiB");
+	test_string_get_size_one(1048064, 1, "1.05 MB", "1.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.0


             reply	other threads:[~2026-04-07 14:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-07 14:33 Shuvam Pandey [this message]
2026-04-07 14:53 ` [PATCH] lib/string_helpers: fix string_get_size() unit promotion after rounding 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=20260407143306.78809-1-shuvampandey1@gmail.com \
    --to=shuvampandey1@gmail.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