Skip to content

Commit 81a1dd1

Browse files
bjorn-rivospalmer-dabbelt
authored andcommitted
riscv, lib: Fix Zbb strncmp
The Zbb optimized strncmp has two parts; a fast path that does XLEN/8B per iteration, and a slow that does one byte per iteration. The idea is to compare aligned XLEN chunks for most of strings, and do the remainder tail in the slow path. The Zbb strncmp has two issues in the fast path: Incorrect remainder handling (wrong compare): Assume that the string length is 9. On 64b systems, the fast path should do one iteration, and one iteration in the slow path. Instead, both were done in the fast path, which lead to incorrect results. An example: strncmp("/dev/vda", "/dev/", 5); Correct by changing "bgt" to "bge". Missing NULL checks in the second string: This could lead to incorrect results for: strncmp("/dev/vda", "/dev/vda\0", 8); Correct by adding an additional check. Fixes: b6fcdb1 ("RISC-V: add zbb support to string functions") Suggested-by: Heiko Stuebner <[email protected]> Signed-off-by: Björn Töpel <[email protected]> Tested-by: Conor Dooley <[email protected]> Tested-by: Guenter Roeck <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 6934cf8 commit 81a1dd1

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

arch/riscv/lib/strncmp.S

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ strncmp_zbb:
7878
/* Main loop for aligned string. */
7979
.p2align 3
8080
1:
81-
bgt a0, t6, 3f
81+
bge a0, t6, 3f
8282
REG_L t0, 0(a0)
8383
REG_L t1, 0(a1)
8484
orc.b t3, t0
8585
bne t3, t5, 2f
86+
orc.b t3, t1
87+
bne t3, t5, 2f
8688
addi a0, a0, SZREG
8789
addi a1, a1, SZREG
8890
beq t0, t1, 1b

0 commit comments

Comments
 (0)