From 448c7632dbe12c0cd0385e25503288d54a86c1e7 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 1 Nov 2023 16:27:58 +0100 Subject: lib: enforce consistent `index()` behavior with empty needle argument On macOS, the `memmem()` function returns `NULL` instead of the expected start of the haystack string when given a zero-length needle argument. Add special case handling for a zero-length needle argument to ensure that the expected offset `0` is returned on all systems. Ref: #176 Suggested-by: Erwan MAS Signed-off-by: Jo-Philipp Wich --- lib.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib.c b/lib.c index a875d46..1d18204 100644 --- a/lib.c +++ b/lib.c @@ -451,12 +451,15 @@ uc_index(uc_vm_t *vm, size_t nargs, bool right) } while (--p != sstr); } - else { + else if (nlen > 0) { p = (const char *)memmem(sstr, slen, nstr, nlen); if (p) ret = (ssize_t)(p - sstr); } + else { + ret = 0; + } } } -- cgit v1.2.3