Skip to content

Commit b3f4f51

Browse files
Villemoespaulmckrcu
authored andcommitted
tools/nolibc/string: Fix memcmp() implementation
The C standard says that memcmp() must treat the buffers as consisting of "unsigned chars". If char happens to be unsigned, the casts are ok, but then obviously the c1 variable can never contain a negative value. And when char is signed, the casts are wrong, and there's still a problem with using an 8-bit quantity to hold the difference, because that can range from -255 to +255. For example, assuming char is signed, comparing two 1-byte buffers, one containing 0x00 and another 0x80, the current implementation would return -128 for both memcmp(a, b, 1) and memcmp(b, a, 1), whereas one of those should of course return something positive. Signed-off-by: Rasmus Villemoes <[email protected]> Fixes: 66b6f75 ("rcutorture: Import a copy of nolibc") Cc: [email protected] # v5.0+ Signed-off-by: Willy Tarreau <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent bfc3b0f commit b3f4f51

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

tools/include/nolibc/string.h

Lines changed: 2 additions & 2 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;

0 commit comments

Comments
 (0)