diff options
author | Jo-Philipp Wich <jo@mein.io> | 2023-11-01 16:27:58 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2023-11-01 16:27:58 +0100 |
commit | 448c7632dbe12c0cd0385e25503288d54a86c1e7 (patch) | |
tree | ea3d45338485af51f9c35e34228284870df0d9c7 | |
parent | cdc02034cfd4ba5f94bf26ee9e9c23609a9c47a4 (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.c | 5 |
1 files changed, 4 insertions, 1 deletions
@@ -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; + } } } |