[PATCH] Fix regression in memrchr

Federico Terraneo fede.tft@hotmail.it
Thu Mar 19 07:55:44 GMT 2026


Hi,
while upgrading our toolchain for the Miosix kernel to use the lastest 
Newlib release, we noticed a regression in memrchr. This issue causes an 
unaligned memory access resulting in a fault in ARM Cortex M0 
microcontrollers.

The bug has been introduced in commit 
c9b74e32892966c8f66280c752181292bc95f690, where the macro UNALIGNED was 
replaced with a generic one UNALIGNED_X.
However, in memrchr.c the UNALIGNED macro was defined as

#define UNALIGNED(X) ((long)(X + 1) & (sizeof (long) - 1))

thus checking if X+1 is aligned, not if X is aligned.

The replacement macro checks if X is aligned, thus breaking the algorithm.

The issue was found by my colleague Daniele Cattaneo.

Best regards,
Federico Terraneo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-libc-string-Fix-off-by-one-alignment-bug-in-memrchr.patch
Type: text/x-patch
Size: 1410 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/newlib/attachments/20260319/547fb199/attachment.bin>


More information about the Newlib mailing list