]> git.baikalelectronics.ru Git - kernel.git/commit
tools/nolibc/string: Fix memcmp() implementation
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Fri, 21 Oct 2022 06:01:53 +0000 (08:01 +0200)
committerPaul E. McKenney <paulmck@kernel.org>
Fri, 28 Oct 2022 22:07:02 +0000 (15:07 -0700)
commitb4bddd5bec8246e9d868d95f0018b5409739b766
tree3ec1de452830951ea199ea29a993f117b89aee2a
parent3524570aca6bcbd0e212c11ddbe8d99abcefb387
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 <linux@rasmusvillemoes.dk>
Fixes: da585b1d4e67 ("rcutorture: Import a copy of nolibc")
Cc: stable@vger.kernel.org # v5.0+
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/string.h