commit 44a3fd53a73262f822613097d7b6a01c70320ba3
parent 75b05d85411ef3d37ef3ab460f1e12c2960cb6a8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sun, 16 Sep 2018 06:40:50 +0100
[lib/c] Fix memcmp()
Char can be signed and the substraction of signed types can produce
unexpected results if it doesn't fit in an integer.
Diffstat:
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/lib/c/memcmp.c b/lib/c/memcmp.c
@@ -4,9 +4,10 @@
int
memcmp(const void *s1, const void *s2, size_t n)
{
- char *s = (char *) s1, *t = (char *) s2;
+ const unsigned char *s = (unsigned char *) s1;
+ const unsigned char *t = (unsigned char *) s2;
- while (n > 0 && *s == *t)
- --n, ++s, ++t;
+ for ( ; n > 0 && *s++ == *t++; --n)
+ ;
return n ? (*s - *t) : 0;
}