• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <string.h>
2 #include <stdint.h>
3 #include <limits.h>
4 
5 #define ALIGN (sizeof(size_t))
6 #define ONES ((size_t)-1/UCHAR_MAX)
7 #define HIGHS (ONES * (UCHAR_MAX/2+1))
8 #define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
9 
10 #ifdef ENABLE_HWASAN
11 __attribute__((no_sanitize("hwaddress")))
12 #endif
__strchrnul(const char * s,int c)13 char *__strchrnul(const char *s, int c)
14 {
15 	c = (unsigned char)c;
16 	if (!c) return (char *)s + strlen(s);
17 
18 #ifdef __GNUC__
19 	typedef size_t __attribute__((__may_alias__)) word;
20 	const word *w;
21 	for (; (uintptr_t)s % ALIGN; s++)
22 		if (!*s || *(unsigned char *)s == c) return (char *)s;
23 	size_t k = ONES * c;
24 	for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w^k); w++);
25 	s = (void *)w;
26 #endif
27 	for (; *s && *(unsigned char *)s != c; s++);
28 	return (char *)s;
29 }
30 
31 weak_alias(__strchrnul, strchrnul);
32