In most cases the last char of the sectio will be zero. Check that
first before calling memrchr. This is a minor optimization in normal
cases. But it helps asan a lot by removing the memrchr call in most
cases.
https://sourceware.org/bugzilla/show_bug.cgi?id=28101
Signed-off-by: Mark Wielaard <mark@klomp.org>
+2021-07-19 Mark Wielaard <mark@klomp.org>
+
+ * elf_strptr.c (validate_str): Check last char is zero first before
+ calling memrchr on the whole block.
+
2021-06-09 Andrei Homescu <ah@immunant.com>
* elf_getdata.c: Fix d_align for sections where alignment is larger
static bool validate_str (const char *str, size_t from, size_t to)
{
#if HAVE_DECL_MEMRCHR
- return memrchr (&str[from], '\0', to - from) != NULL;
+ // Check end first, which is likely a zero terminator, to prevent function call
+ return ((to > 0 && str[to - 1] == '\0')
+ || (to - from > 0 && memrchr (&str[from], '\0', to - from - 1) != NULL));
#else
do {
if (to <= from)