summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillaume Knispel <xilun0@gmail.com>2018-01-06 01:11:31 +0100
committerRichard Braun <rbraun@sceen.net>2018-01-06 01:37:40 +0100
commit0f44aeee1c3808ef82e49afb984a2ff9c25223bf (patch)
tree3ba10006edcea5bbfbe7a80fd929e2e893f60064
parentb5b73ee57177200571ba1f1c8d0a4e591a941558 (diff)
string: fix strcmp and strncmp
-rw-r--r--src/string.c13
1 files 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 *