From 0f44aeee1c3808ef82e49afb984a2ff9c25223bf Mon Sep 17 00:00:00 2001 From: Guillaume Knispel Date: Sat, 6 Jan 2018 01:11:31 +0100 Subject: string: fix strcmp and strncmp --- src/string.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/string.c b/src/string.c index 7d737c7..60ac49e 100644 --- a/src/string.c +++ b/src/string.c @@ -118,7 +118,15 @@ strcmp(const char *s1, const char *s2) s2++; } - return (int)c1 - (int)c2; + /* + * See C99 7.21.4 Comparison functions : + * The sign of a nonzero value returned by the comparison functions + * memcmp, strcmp, and strncmp is determined by the sign of the + * difference between the values of the first pair of characters + * (both interpreted as unsigned char) that differ in the objects + * being compared. + */ + return (int)(unsigned char)c1 - (int)(unsigned char)c2; } int @@ -140,7 +148,8 @@ strncmp(const char *s1, const char *s2, size_t n) s2++; } - return (int)c1 - (int)c2; + /* See strcmp() */ + return (int)(unsigned char)c1 - (int)(unsigned char)c2; } char * -- cgit v1.2.3