From nobody Sun Jun 21 04:17:36 2026 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37BD325A2B5 for ; Tue, 7 Apr 2026 14:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775572394; cv=none; b=KJVd8LhU55zmLjT8r3+KtHk1EZLif+U1hV41mo1rn/6233mXUqmxBiGooL1V9JiwnAeClZM1tY54glALhx4JaK/pO2x49+5p9VyYl7WrK3amkCaITWFmB1XqL/T7Eao1K0Dpp2k5Z8AvxgQ5xDYO/PEjamOjSjL7fQMHQLsXtjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775572394; c=relaxed/simple; bh=y2ryRWTMom41Ry8K7iEMHsKVi693F6fn5CC+z8ttHYk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=GEfB/kMZCP+CKQm7LDupTrxjCp7dMwgcve+bXEx/N/6aFzsGk52KJwBD6eo109una7XyBRa8Ui00SXTPeaawqL5ePXz9m3WuYW5kTiFJkX8WRQgSfS4tmPQljC5/yKOt1rps0JTSK+eAF/1keC9aF5GjQn59bkgIdmIL3BxM0DI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eBwgUyJL; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eBwgUyJL" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-823c56765fdso2625363b3a.1 for ; Tue, 07 Apr 2026 07:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775572392; x=1776177192; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=S06L4O8V2fqzeDXiPwpWBh6KNFIVpJ0g//Xhqb4k9Lw=; b=eBwgUyJLv5Zl6MPFArvWfUKRzax8VW3IPkezzwSF2BQulMeVb5ratrjpeAmh7wRWxc ikNGnuySHTflqDWquqKjnRkCdgxymxV9DTqSGLbUEZrCZktac2vh3LKJ4qxJHwYGslyI yP6OLIZd38ixtmt9ixSJwId1BTv84eJVlAGeGD+kdOV72EcYlxJqH4GfPxTUjAR19VwF sLwl086wFLWoOR0a/E6/OLlDreNJMHdFGSZ58pg0tFFHoihXaelcFnlTZ9ot6PdkOj/X tcTKFS98gr19i1YRj4xIK2jeKe4ifI0F5OXVwCZIYBJPrKlcS4TaT+kLBxOMg9QjC5Go H/zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775572392; x=1776177192; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=S06L4O8V2fqzeDXiPwpWBh6KNFIVpJ0g//Xhqb4k9Lw=; b=kDxTpoXzMJdPfW4rbD+BFXYGIsVOrKzvDojUGcVITmeQRZDgPuzJesOG4NzbwHGxIf EX1GaWN+S7aA0QjO2WXK2KWz4dNvMQgbD8/1jCdkADpEw7p7WHff/OEJZBFLQIB4nwwC GDRdUyfYGJVXKKfHP78rzezzwYJ4e+TMr2ODJBcrlhOltPsXRPbmTuSGhjItsDYOLH07 s7/naO6RC3omloqkK1vlPVVEjlpgVX5ihnswldzgVUgWOXRnWpQZVJd24JZ3cyWpdmmG O6eFMU8P47lwR1cyNhptt64bAYSXj6Lcbn6hanOkMK6uennTUG1Sh0MJldBEsA8OJv6L OnHw== X-Gm-Message-State: AOJu0Ywue0TndrZg767NeyjlczF8amBOQzCfeJIAgYIB+jcCSnOj8aVJ aZEFspZPglKM2ZL7zV5HVO2VLLgaQ4sMorgQaoB3qLkgNx7seJO2Kakf X-Gm-Gg: AeBDiev1Qdv+l30c8sckAHNBJGVDtGnQ2IiOv/Ie45dG7tHwuOzY5Q3yXOGhHYRVkJi iXtZK9GLwyUyuJwbaSllo87BZ++XqUbY0YYSXNHaLC///z5A4ZytpGZUeurnOFA8r/mLmmddJSP SwmP9/B8I182QGrGgWNlW/gd3AUpwk4fh14OxBUgX2LnuJfjD8LK5wCuBCjw7HSiSZaI0dwTS9g I1HIJu+NYgR2RWafQUTjOuq7zB+wevVUVVsbcKt3CXyoK6r2HRyECGUJ8aLMju2/4uzXnDmSrpE 3xAsU0DbROE/CyQ6GzH6FUIxcpDJ1/+9fSLF2Xjm1kRpsSLHRurW71Yiaamj6bPeTqZ8J3eFmox duk1ZD4aboee/EgjUuqXQ1mwPqc1SPHMErE3+KTuZDaH1m9wKmvVrfOVDh59hg8qlWBiOWls6/z Z1zYhGx3RtFWMb4bj/eeYaBwkFgZRE4wNYhRLDnU/rsGmhev2J5u1uaHtywGd4re6ptOwH7Ml/v 8DXbYy15QvT2T5hxEg= X-Received: by 2002:a05:6a00:848:b0:82a:7471:eb90 with SMTP id d2e1a72fcca58-82d0db71c03mr19798857b3a.30.1775572392342; Tue, 07 Apr 2026 07:33:12 -0700 (PDT) Received: from localhost.localdomain ([103.10.31.106]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82cf9ca75dfsm17611419b3a.60.2026.04.07.07.33.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 07 Apr 2026 07:33:11 -0700 (PDT) From: Shuvam Pandey To: Andrew Morton , Kees Cook , Andy Shevchenko 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 Message-ID: <20260407143306.78809-1-shuvampandey1@gmail.com> X-Mailer: git-send-email 2.50.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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 +=3D 1; } =20 + if (size >=3D divisor[units_base]) { + size =3D 1; + remainder =3D 0; + i++; + j =3D 2; + } + if (j) { snprintf(tmp, sizeof(tmp), ".%03u", remainder); tmp[j+1] =3D '\0'; diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_ku= nit.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"); =20 + /* 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"); --=20 2.50.0