:: commit 22df06c05754fba9e0be55a92a55687fd241fd84

Mintsuki <mintsuki@protonmail.com> — 2025-12-02 02:49

parents: bf3c4a658c

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;
tab: 248 wrap: offon