Skip to content

Commit

Permalink
lib: enforce consistent index() behavior with empty needle argument
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Jo-Philipp Wich <[email protected]>
  • Loading branch information
jow- committed Nov 1, 2023
1 parent cdc0203 commit 448c763
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 448c763

Please sign in to comment.