From patchwork Mon Jul 19 08:10:11 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: mark at klomp dot org X-Patchwork-Id: 45838 From: sourceware-bugzilla@sourceware.org (mark at klomp dot org) Date: Mon, 19 Jul 2021 08:10:11 +0000 Subject: [Bug libelf/28101] elf_strptr slow with address sanitizer, passes entire section range to memrchr. In-Reply-To: References: Message-ID: https://sourceware.org/bugzilla/show_bug.cgi?id=28101 Mark Wielaard changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |mark at klomp dot org --- Comment #1 from Mark Wielaard --- I think it really is a bug/performance issue in asan. But "optimizing" it in libelf by first checking the last char is zero, before calling memrchr wouldn't hurt (and should normally prevent a function call). Does the following help? do { if (to <= from) diff --git a/libelf/elf_strptr.c b/libelf/elf_strptr.c index 76f2caf1..dc9b76c0 100644 --- a/libelf/elf_strptr.c +++ b/libelf/elf_strptr.c @@ -56,7 +56,9 @@ get_zdata (Elf_Scn *strscn) 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 (str[to - 1] == '\0' + || (to - from > 0 && memrchr (&str[from], '\0', to - from - 1) != NULL)); #else