Skip to content

Commit 54917c9

Browse files
committed
Merge tag 'nolibc-urgent.2022.10.28a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu
Pull nolibc fixes from Paul McKenney: "This contains a couple of fixes for string-function bugs" * tag 'nolibc-urgent.2022.10.28a' of git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu: tools/nolibc/string: Fix memcmp() implementation tools/nolibc: Fix missing strlen() definition and infinite loop with gcc-12
2 parents f526d6a + b3f4f51 commit 54917c9

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

tools/include/nolibc/string.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ static __attribute__((unused))
1919
int memcmp(const void *s1, const void *s2, size_t n)
2020
{
2121
size_t ofs = 0;
22-
char c1 = 0;
22+
int c1 = 0;
2323

24-
while (ofs < n && !(c1 = ((char *)s1)[ofs] - ((char *)s2)[ofs])) {
24+
while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
2525
ofs++;
2626
}
2727
return c1;
@@ -125,28 +125,31 @@ char *strcpy(char *dst, const char *src)
125125
}
126126

127127
/* this function is only used with arguments that are not constants or when
128-
* it's not known because optimizations are disabled.
128+
* it's not known because optimizations are disabled. Note that gcc 12
129+
* recognizes an strlen() pattern and replaces it with a jump to strlen(),
130+
* thus itself, hence the asm() statement below that's meant to disable this
131+
* confusing practice.
129132
*/
130133
static __attribute__((unused))
131-
size_t nolibc_strlen(const char *str)
134+
size_t strlen(const char *str)
132135
{
133136
size_t len;
134137

135-
for (len = 0; str[len]; len++);
138+
for (len = 0; str[len]; len++)
139+
asm("");
136140
return len;
137141
}
138142

139143
/* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
140144
* the two branches, then will rely on an external definition of strlen().
141145
*/
142146
#if defined(__OPTIMIZE__)
147+
#define nolibc_strlen(x) strlen(x)
143148
#define strlen(str) ({ \
144149
__builtin_constant_p((str)) ? \
145150
__builtin_strlen((str)) : \
146151
nolibc_strlen((str)); \
147152
})
148-
#else
149-
#define strlen(str) nolibc_strlen((str))
150153
#endif
151154

152155
static __attribute__((unused))

0 commit comments

Comments
 (0)