From c25724dc73a4a81cd610508da9e63cf4304dea25 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Wed, 22 Nov 2000 21:18:34 +0000 Subject: Fix a bug in memcmp -- don't terminate the compare on a NULL, keep comparing up to n bytes, till the are found to be either the same or differernt. --- libc/string/string.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/libc/string/string.c b/libc/string/string.c index eb5c17709..fdf848125 100644 --- a/libc/string/string.c +++ b/libc/string/string.c @@ -2,6 +2,9 @@ /* Copyright (C) 1995,1996 Robert de Bath * This file is part of the Linux-8086 C library and is distributed * under the GNU Library General Public License. + * + * Many of the functions in this file have been rewritten for correctness + * (but not necessarily speed) by Erik Andersen */ #include @@ -263,21 +266,16 @@ void *memchr(const void *str, int c, size_t len) #ifdef L_memcmp int memcmp(const void *s1, const void *s2, size_t len) { - unsigned register char c1 = '\0'; - unsigned register char c2 = '\0'; - - register char *str1 = (char *) s1; - register char *str2 = (char *) s2; + unsigned char *c1 = (unsigned char *)s1; + unsigned char *c2 = (unsigned char *)s2; - while (len > 0) { - c1 = (unsigned char) *str1++; - c2 = (unsigned char) *str2++; - if (c1 == '\0' || c1 != c2) - return c1 - c2; - len--; + while (len--) { + if (*c1 != *c2) + return *c1 - *c2; + c1++; + c2++; } - - return c1 - c2; + return 0; } #endif -- cgit v1.2.3