From c2383f9c821495c5803a1a88360544c15cad519f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 19 Mar 2008 08:45:08 +0000 Subject: [PATCH] * Replaced our string compare functions (and memcmp()) with versions that actually work correctly (and treat the data as unsigned characters). * This fixes bug #724. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24458 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/string/memcmp.c | 27 +++++++++++--------- src/system/libroot/posix/string/strcasecmp.c | 5 ++-- src/system/libroot/posix/string/strcmp.c | 24 ++++++++--------- src/system/libroot/posix/string/strncmp.c | 24 ++++++++--------- 4 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/system/libroot/posix/string/memcmp.c b/src/system/libroot/posix/string/memcmp.c index 982e47fbb5..f5034c0beb 100644 --- a/src/system/libroot/posix/string/memcmp.c +++ b/src/system/libroot/posix/string/memcmp.c @@ -1,20 +1,23 @@ -/* -** Copyright 2001, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ +/* + * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT license. + */ + -#include #include int -memcmp(const void *cs, const void *ct, size_t count) +memcmp(const void *_a, const void *_b, size_t count) { - const unsigned char *su1, *su2; - signed char res = 0; + const unsigned char *a = (const unsigned char *)_a; + const unsigned char *b = (const unsigned char *)_b; - for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) - if ((res = *su1 - *su2) != 0) - break; - return res; + while (count-- > 0) { + int cmp = *a++ - *b++; + if (cmp != 0) + return cmp; + } + + return 0; } diff --git a/src/system/libroot/posix/string/strcasecmp.c b/src/system/libroot/posix/string/strcasecmp.c index d584ddd7dc..d54cf79b57 100644 --- a/src/system/libroot/posix/string/strcasecmp.c +++ b/src/system/libroot/posix/string/strcasecmp.c @@ -35,8 +35,6 @@ #include #include -//typedef unsigned char u_char; - int strcasecmp(const char *s1, const char *s2) @@ -44,9 +42,10 @@ strcasecmp(const char *s1, const char *s2) const u_char *us1 = (const u_char *)s1; const u_char *us2 = (const u_char *)s2; - while (tolower(*us1) == tolower(*us2++)) + while (tolower(*us1) == tolower(*us2++)) { if (*us1++ == '\0') return 0; + } return tolower(*us1) - tolower(*--us2); } diff --git a/src/system/libroot/posix/string/strcmp.c b/src/system/libroot/posix/string/strcmp.c index 38a9cac7f2..63551164dc 100644 --- a/src/system/libroot/posix/string/strcmp.c +++ b/src/system/libroot/posix/string/strcmp.c @@ -1,21 +1,19 @@ -/* -** Copyright 2001, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ +/* + * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT license. + */ -#include + +#include #include int -strcmp(char const *cs, char const *ct) +strcmp(char const *a, char const *b) { - signed char __res; - - while (1) { - if ((__res = *cs - *ct++) != 0 || !*cs++) - break; + while (true) { + int cmp = (unsigned char)*a - (unsigned char)*b++; + if (cmp != 0 || *a++ == '\0') + return cmp; } - - return __res; } diff --git a/src/system/libroot/posix/string/strncmp.c b/src/system/libroot/posix/string/strncmp.c index 1bb7718476..43ca5a5d92 100644 --- a/src/system/libroot/posix/string/strncmp.c +++ b/src/system/libroot/posix/string/strncmp.c @@ -1,22 +1,20 @@ -/* -** Copyright 2001, Travis Geiselbrecht. All rights reserved. -** Distributed under the terms of the NewOS License. -*/ +/* + * Copyright 2008, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT license. + */ + -#include #include int -strncmp(char const *cs, char const *ct, size_t count) +strncmp(char const *a, char const *b, size_t count) { - signed char __res = 0; - - while (count > 0) { - if ((__res = *cs - *ct++) != 0 || !*cs++) - break; - count--; + while (count-- > 0) { + int cmp = (unsigned char)*a - (unsigned char)*b++; + if (cmp != 0 || *a++ == '\0') + return cmp; } - return __res; + return 0; }