HomeFreeBSD

libc/amd64/strrchr.S: rewrite and fix scalar implementation

Description

libc/amd64/strrchr.S: rewrite and fix scalar implementation

The original scalar implementation of strrchr() had incorrect
logic that failed if the character searched for was the NUL
character. It was also possibly affected by the issue fixed
in 3d8ef251a for strchrnul().

Rewrite the function with logic that actually works. We defer
checking for the character until after we have checked for NUL.
When we encounter the final NUL byte, we mask out the characters
beyond the tail before checking for a match.

This bug only affects users running on amd64 with ARCHLEVEL=scalar
(cf. simd(7)). The default configuration is not affected.

The bug was unfortunately not caught by the unit test inherited
from NetBSD. An extended unit test catching the issue is proposed
in D56037.

PR: 293915
Reported by: safonov.paul@gmail.com
Tested by: safonov.paul@gmail.com
Fixes: 2ed514a220edbac6ca5ec9f40a3e0b3f2804796d
See also: https://reviews.freebsd.org/D56037
MFC after: 1 week

Details

Provenance
fuzAuthored on Mar 22 2026, 12:37 PM
Parents
rG9b98c4b05391: diff: handle read(2) failures
Branches
Unknown
Tags
Unknown