ref: f4347d0c4b3eed5f0a65917453cfddd9dca1cb7a
parent: 51b5a24e09ca25aa34dfb8cc50299ee75cfd06c1
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Sep 16 02:40:50 EDT 2018
[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.
--- 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;
}