libc: Fix memcmp implementation
The previous implementation would read one byte past both buffers if
the buffers were equal. Make sure the pointers are not incremented
past the last byte. Also ensure that comparing zero bytes always
returns 0.
Change-Id: I5ef25d6bd2f7417b60102dc1c2602d8b23c4c1bc
Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
diff --git a/lib/libc/minimal/source/string/string.c b/lib/libc/minimal/source/string/string.c
index 7218e33..f93f526 100644
--- a/lib/libc/minimal/source/string/string.c
+++ b/lib/libc/minimal/source/string/string.c
@@ -164,10 +164,12 @@
const char *c1 = m1;
const char *c2 = m2;
- while ((n > 0) && (*c1 == *c2)) {
+ if (!n)
+ return 0;
+
+ while ((--n > 0) && (*c1 == *c2)) {
c1++;
c2++;
- n--;
}
return *c1 - *c2;