mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Rafael V. Volkmer" <rafael.v.volkmer@gmail.com>
To: Kees Cook <kees@kernel.org>
Cc: "Gustavo A . R . Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Rafael V. Volkmer" <rafael.v.volkmer@gmail.com>
Subject: [PATCH 2/2] overflow: add size_shl() helper for saturated left shifts
Date: Fri, 28 Nov 2025 00:38:30 -0300	[thread overview]
Message-ID: <20251128033830.331426-3-rafael.v.volkmer@gmail.com> (raw)
In-Reply-To: <20251128033830.331426-1-rafael.v.volkmer@gmail.com>

Introduce size_shl() as a sibling to size_mul(), size_add() and size_sub()
for the left-shift case. It computes value << shift with both operands
promoted to size_t and uses check_shl_overflow() to detect invalid or
overflowing shifts, returning SIZE_MAX on failure.

No functional change for existing callers; this only adds a new helper.

Signed-off-by: Rafael V. Volkmer <rafael.v.volkmer@gmail.com>
---
 include/linux/overflow.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index ca8252e625d5..91d0b5b00a56 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -404,6 +404,25 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
 	return bytes;
 }
 
+/**
+ * size_shl() - Calculate size_t left shift with saturation at SIZE_MAX
+ * @value: value to be shifted
+ * @shift: how many bits left to shift
+ *
+ * Returns: calculate @value << @shift, both promoted to size_t, with any
+ * overflow or invalid shift causing the return value to be SIZE_MAX. The
+ * lvalue must be size_t to avoid implicit type conversion.
+ */
+static inline size_t __must_check size_shl(size_t value, size_t shift)
+{
+	size_t out;
+
+	if (check_shl_overflow(value, shift, &out))
+		return SIZE_MAX;
+
+	return out;
+}
+
 /**
  * array_size() - Calculate size of 2-dimensional array.
  * @a: dimension one
-- 
2.43.0


  parent reply	other threads:[~2025-11-28  3:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-28  3:38 [PATCH 0/2] overflow: extend shift helpers and add size_shl() Rafael V. Volkmer
2025-11-28  3:38 ` [PATCH 1/2] overflow: make check_shl_overflow() 128-bit aware Rafael V. Volkmer
2025-11-28  3:38 ` Rafael V. Volkmer [this message]
2026-01-16  0:43 ` [PATCH 0/2] overflow: extend shift helpers and add size_shl() Kees Cook

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=20251128033830.331426-3-rafael.v.volkmer@gmail.com \
    --to=rafael.v.volkmer@gmail.com \
    --cc=gustavoars@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®