[PATCH v2] lib: string_helpers: silence snprintf() output truncation warning

Bartosz Golaszewski posted 1 patch 3 weeks, 1 day ago
lib/string_helpers.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v2] lib: string_helpers: silence snprintf() output truncation warning
Posted by Bartosz Golaszewski 3 weeks, 1 day ago
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

The output of ".%03u" with the unsigned int in range [0, 4294966295] may
get truncated if the target buffer is not 12 bytes. This can't really
happen here as the 'remainder' variable cannot exceed 999 but the
compiler doesn't know it. To make it happy just increase the buffer to
where the warning goes away.

Fixes: 3c9f3681d0b4 ("[SCSI] lib: add generic helper to print sizes rounded to the correct SI range")
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Reviewed-by: Andy Shevchenko <andy@kernel.org>
Cc: James E.J. Bottomley <James.Bottomley@HansenPartnership.com>
Cc: Kees Cook <kees@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---
Changes in v2:
- improve the commit message

 lib/string_helpers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 4f887aa62fa0..91fa37b5c510 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -57,7 +57,7 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
 	static const unsigned int rounding[] = { 500, 50, 5 };
 	int i = 0, j;
 	u32 remainder = 0, sf_cap;
-	char tmp[8];
+	char tmp[12];
 	const char *unit;
 
 	tmp[0] = '\0';
-- 
2.45.2
Re: [PATCH v2] lib: string_helpers: silence snprintf() output truncation warning
Posted by Kees Cook 3 weeks ago
On Fri, 01 Nov 2024 21:54:53 +0100, Bartosz Golaszewski wrote:
> The output of ".%03u" with the unsigned int in range [0, 4294966295] may
> get truncated if the target buffer is not 12 bytes. This can't really
> happen here as the 'remainder' variable cannot exceed 999 but the
> compiler doesn't know it. To make it happy just increase the buffer to
> where the warning goes away.
> 
> 
> [...]

Applied to for-next/hardening, thanks!

[1/1] lib: string_helpers: silence snprintf() output truncation warning
      https://git.kernel.org/kees/c/a508ef4b1dcc

Take care,

-- 
Kees Cook