]> 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)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 10 Nov 2022 16:57:55 +0000 (17:57 +0100)
commite5ad167f109312b44337798f4016a3f1674cf2b0
tree33dc0f35e0c2be16dbd2d8bf4917de87e8adf712
parentda141ccc9068689eaf883ce840616b9f4683d4e3
tools/nolibc/string: Fix memcmp() implementation

commit d8ded8f4724c83279823231b65c9bac2deb62d40 upstream.

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: 6c9adb75b7b3 ("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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
tools/include/nolibc/nolibc.h