summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2023-11-01 16:27:58 +0100
committerJo-Philipp Wich <jo@mein.io>2023-11-01 16:27:58 +0100
commit448c7632dbe12c0cd0385e25503288d54a86c1e7 (patch)
treeea3d45338485af51f9c35e34228284870df0d9c7
parentcdc02034cfd4ba5f94bf26ee9e9c23609a9c47a4 (diff)
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 <erwan@mas.nom.fr> Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--lib.c5
1 files changed, 4 insertions, 1 deletions
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;
+ }
}
}