aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBen Hutchings <ben@decadent.org.uk>2022-12-30 22:48:59 +0100
committerBen Hutchings <ben@decadent.org.uk>2022-12-30 23:01:22 +0100
commitbf50476c204f7eab1fba5370a49fb03fcfaf92d1 (patch)
tree4d548ebf3d3a9f886bc6e86e0eb1dbbb043594e6
parent61d2ea539c88f7862b3992b9a00daaedb6bb68ef (diff)
downloadklibc-bf50476c204f7eab1fba5370a49fb03fcfaf92d1.tar.gz
[klibc] strstr, memmem: Handle zero-length needle correctly
strstr(haystack, "") and memmem(haystack, n, needle, 0) should return haystack, not NULL. - Handle the !m and (m > n || !n) cases separately at the top. - Delete the !n condition. After handling !m we know m > 0, so checking for !n is redundant with m > n. Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
-rw-r--r--usr/klibc/memmem.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/usr/klibc/memmem.c b/usr/klibc/memmem.c
index 8b5faa0014e8ad..72422aa4b8628b 100644
--- a/usr/klibc/memmem.c
+++ b/usr/klibc/memmem.c
@@ -18,7 +18,10 @@ void *memmem(const void *haystack, size_t n, const void *needle, size_t m)
size_t j, k, l;
- if (m > n || !m || !n)
+ if (!m)
+ return (void *)haystack;
+
+ if (m > n)
return NULL;
if (1 != m) {