Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions arch/riscv/lib/strnlen.S
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SYM_FUNC_START(strnlen)
__ALTERNATIVE_CFG("nop", "j strnlen_zbb", 0, RISCV_ISA_EXT_ZBB,
IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZBB))

strnlen_generic:

/*
* Returns
Expand All @@ -27,15 +28,17 @@ SYM_FUNC_START(strnlen)
* a1 - Max length of string
*
* Clobbers
* t0, t1, t2
* t0, t1
*/
addi t1, a0, -1
add t2, a0, a1
mv t1, a0

1:
addi t1, t1, 1
beq t1, t2, 2f
beqz a1, 2f
addi a1, a1, -1
lbu t0, 0(t1)
bnez t0, 1b
beqz t0, 2f
addi t1, t1, 1
j 1b
2:
sub a0, t1, a0
ret
Expand Down Expand Up @@ -73,6 +76,13 @@ strnlen_zbb:
/* If maxlen is 0, return 0. */
beqz a1, 3f

/*
* Fallback to generic implementation when count is large enough to
* cause address overflow in the ZBB optimized path
*/
add t4, a0, a1
bltu t4, a0, strnlen_generic /* a0 + a1 overflow */

/* Number of irrelevant bytes in the first word. */
andi t2, a0, SZREG-1

Expand Down
Loading