lib/libc: Convert char to unsigned char before comparison in string comparison functions
diff --git a/common/lib/libc.s2.c b/common/lib/libc.s2.c
index 11bd4461..e4c5ef3d 100644
--- a/common/lib/libc.s2.c
+++ b/common/lib/libc.s2.c
@@ -62,31 +62,37 @@ char *strncpy(char *dest, const char *src, size_t n) {
int strcmp(const char *s1, const char *s2) {
for (size_t i = 0; ; i++) {
- char c1 = s1[i], c2 = s2[i];
- if (c1 != c2)
+ unsigned char c1 = ((unsigned char *)s1)[i], c2 = ((unsigned char *)s2)[i];
+ if (c1 != c2) {
return c1 < c2 ? -1 : 1;
- if (!c1)
+ }
+ if (c1 == 0) {
return 0;
+ }
}
}
int strcasecmp(const char *s1, const char *s2) {
for (size_t i = 0; ; i++) {
- char c1 = s1[i], c2 = s2[i];
- if (tolower(c1) != tolower(c2))
+ unsigned char c1 = ((unsigned char *)s1)[i], c2 = ((unsigned char *)s2)[i];
+ if (tolower(c1) != tolower(c2)) {
return c1 < c2 ? -1 : 1;
- if (!c1)
+ }
+ if (c1 == 0) {
return 0;
+ }
}
}
int strncmp(const char *s1, const char *s2, size_t n) {
for (size_t i = 0; i < n; i++) {
- char c1 = s1[i], c2 = s2[i];
- if (c1 != c2)
+ unsigned char c1 = ((unsigned char *)s1)[i], c2 = ((unsigned char *)s2)[i];
+ if (c1 != c2) {
return c1 < c2 ? -1 : 1;
- if (!c1)
+ }
+ if (c1 == 0) {
return 0;
+ }
}
return 0;
@@ -94,11 +100,13 @@ int strncmp(const char *s1, const char *s2, size_t n) {
int strncasecmp(const char *s1, const char *s2, size_t n) {
for (size_t i = 0; i < n; i++) {
- char c1 = s1[i], c2 = s2[i];
- if (tolower(c1) != tolower(c2))
+ unsigned char c1 = ((unsigned char *)s1)[i], c2 = ((unsigned char *)s2)[i];
+ if (tolower(c1) != tolower(c2)) {
return c1 < c2 ? -1 : 1;
- if (!c1)
+ }
+ if (c1 == 0) {
return 0;
+ }
}
return 0;
