@@ -113,7 +113,7 @@ void *memchr(const void *v, int c, size_t n) {
113113 // That's a match, unless it is beyond the end of the object.
114114 // Recall that we decremented n, so less-than-or-equal-to is correct.
115115 size_t ctz = __builtin_ctz (mask );
116- return ctz <= n + align ? (char * )w + ctz : NULL ;
116+ return ctz - align <= n ? (char * )w + ctz : NULL ;
117117 }
118118 }
119119 // Decrement n; if it overflows we're done.
@@ -166,6 +166,8 @@ size_t strlen(const char *s) {
166166 // At least one bit will be set, unless we cleared them.
167167 // Knowing this helps the compiler.
168168 __builtin_assume (mask || align );
169+ // If the mask is zero because of alignment,
170+ // it's as if we didn't find anything.
169171 if (mask ) {
170172 // Find the offset of the first one bit (little-endian).
171173 return (char * )w - s + __builtin_ctz (mask );
@@ -280,6 +282,8 @@ static char *__strchrnul(const char *s, int c) {
280282 // At least one bit will be set, unless we cleared them.
281283 // Knowing this helps the compiler.
282284 __builtin_assume (mask || align );
285+ // If the mask is zero because of alignment,
286+ // it's as if we didn't find anything.
283287 if (mask ) {
284288 // Find the offset of the first one bit (little-endian).
285289 return (char * )w + __builtin_ctz (mask );
0 commit comments