:: commit 3487bc14f0aac3944aab0080508c3b2d7d2af700

Mintsuki <mintsuki@protonmail.com> — 2025-12-26 10:32

parents: 1b5f95c2ac

lib/libc: Fix strchr and strrchr null terminator handling and add inet_pton() sanity checks

diff --git a/common/lib/libc.c b/common/lib/libc.c
index 85831676..1e654dda 100644
--- a/common/lib/libc.c
+++ b/common/lib/libc.c
@@ -97,22 +97,26 @@ void *memchr(const void *ptr, int ch, size_t n) {
 }
 
 char *strchr(const char *str, int ch) {
-    for (size_t i = 0; str[i]; i++) {
-        if (str[i] == ch) {
+    for (size_t i = 0; ; i++) {
+        if (str[i] == (char)ch) {
             return (char *)str + i;
         }
+        if (str[i] == '\0') {
+            return NULL;
+        }
     }
-
-    return NULL;
 }
 
 char *strrchr(const char *str, int ch) {
     char *p = NULL;
 
-    for (size_t i = 0; str[i]; i++) {
-        if (str[i] == ch) {
+    for (size_t i = 0; ; i++) {
+        if (str[i] == (char)ch) {
             p = (char *)str + i;
         }
+        if (str[i] == '\0') {
+            break;
+        }
     }
 
     return p;
diff --git a/common/lib/libc.s2.c b/common/lib/libc.s2.c
index e4c5ef3d..ec2ca8c9 100644
--- a/common/lib/libc.s2.c
+++ b/common/lib/libc.s2.c
@@ -130,11 +130,18 @@ int inet_pton(const char *src, void *dst) {
         if (current == newcur)
             return -1;
         current = newcur;
-        if (*current == 0 && i < 3)
-            return -1;
+        if (i < 3) {
+            // Expect '.' delimiter between octets
+            if (*current != '.')
+                return -1;
+            current++;
+        } else {
+            // After last octet, string must end
+            if (*current != 0)
+                return -1;
+        }
         if (value > 255)
             return -1;
-        current++;
         array[i] = value;
     }
     memcpy(dst, array, 4);
tab: 248 wrap: offon