This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push: new 47026978bf libs/libc/string: fix memmem() boundary case when needle is at end of haystack 47026978bf is described below commit 47026978bf7d1f3db6f86a98a8e6ba73024f9489 Author: Juha Niskanen <juha.niska...@haltian.com> AuthorDate: Mon Mar 11 12:40:43 2024 +0200 libs/libc/string: fix memmem() boundary case when needle is at end of haystack This fixes calls like memmem("hello", 5, "lo", 2); Also zero-length needle is deemed to exist at beginning of haystack. This behavior matches memmem() on Linux, FreeBSD, NetBSD and OpenBSD. Signed-off-by: Juha Niskanen <juha.niska...@haltian.com> --- libs/libc/string/lib_memmem.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libs/libc/string/lib_memmem.c b/libs/libc/string/lib_memmem.c index 77dbd4b357..f54ea9817c 100644 --- a/libs/libc/string/lib_memmem.c +++ b/libs/libc/string/lib_memmem.c @@ -51,12 +51,17 @@ FAR void *memmem(FAR const void *haystack, size_t haystacklen, size_t i; size_t y; + if (needlelen == 0) + { + return (void *)haystack; + } + if (needlelen > haystacklen) { return NULL; } - for (i = 0; i < haystacklen - needlelen; i++) + for (i = 0; i <= haystacklen - needlelen; i++) { y = 0; while (h[i + y] == n[y])