From 88b8ca5b29a41842d09af2c8d934e7cb241af62b Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 13 Oct 2024 22:04:08 +0300 Subject: [PATCH] LibC: Fix some string functions I was not casting some required values to char or handling length of zero --- userspace/libraries/LibC/string.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/userspace/libraries/LibC/string.cpp b/userspace/libraries/LibC/string.cpp index fd17f356..40ed2ef5 100644 --- a/userspace/libraries/LibC/string.cpp +++ b/userspace/libraries/LibC/string.cpp @@ -87,6 +87,8 @@ int strcmp(const char* s1, const char* s2) int strncmp(const char* s1, const char* s2, size_t n) { + if (n == 0) + return 0; const unsigned char* u1 = (unsigned char*)s1; const unsigned char* u2 = (unsigned char*)s2; for (; --n && *u1 && *u2; u1++, u2++) @@ -220,11 +222,11 @@ char* strchr(const char* str, int c) { while (*str) { - if (*str == c) + if (*str == (char)c) return (char*)str; str++; } - return (*str == c) ? (char*)str : nullptr; + return (*str == (char)c) ? (char*)str : nullptr; } char* strchrnul(const char* str, int c) @@ -252,7 +254,9 @@ char* strrchr(const char* str, int c) char* strstr(const char* haystack, const char* needle) { - size_t needle_len = strlen(needle); + const size_t needle_len = strlen(needle); + if (needle_len == 0) + return const_cast(haystack); for (size_t i = 0; haystack[i]; i++) if (strncmp(haystack + i, needle, needle_len) == 0) return const_cast(haystack + i);