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 <[email protected]> --- 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
