[PATCH 0/2] riscv: strnlen ZBB overflow fix and test coverage

From: Gao Rui

Date: Wed Jul 22 2026 - 02:50:17 EST


Hi all,

I noticed the earlier patch from Michael Neuling:
https://lore.kernel.org/linux-riscv/20260413010738.1622423-1-mikey@xxxxxxxxxxx/
which adjusted the boundary calculation in strnlen. However, that fix
is not complete — in many cases the `count` value itself can overflow,
leading to incorrect behavior. This series addresses those missing
cases by adding explicit fallback logic for SIZE_MAX and overflow
conditions, and extends KUnit coverage to validate the new behavior.

Patch 1/2:
riscv: strnlen: fix ZBB path overflow and SIZE_MAX fallback
- Add fallback to generic path when count == SIZE_MAX
- Add overflow check for (s + count)
- Refactor aligned boundary calculation
- Add fast exit for first word case
- Simplify generic path loop

Patch 2/2:
kunit: string: add strnlen SIZE_MAX test coverage
- Add explicit test case for strnlen(s, SIZE_MAX)
- Ensure fallback returns correct length for non-terminated strings
- Complements existing boundary tests

Gao Rui (2):
riscv: strnlen: fix ZBB path overflow and SIZE_MAX fallback
kunit: string: add strnlen SIZE_MAX test coverage

arch/riscv/lib/strnlen.S | 40 +++++++++++++++++++++++++++++++---------
lib/tests/string_kunit.c | 13 +++++++++++++
2 files changed, 44 insertions(+), 9 deletions(-)

--
2.27.0