]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[libbacktrace] Handle bsearch with NULL base in dwarf_lookup_pc
authorTom de Vries <tdevries@suse.de>
Tue, 12 Feb 2019 14:00:59 +0000 (14:00 +0000)
committerTom de Vries <vries@gcc.gnu.org>
Tue, 12 Feb 2019 14:00:59 +0000 (14:00 +0000)
The call to bsearch in dwarf_lookup_pc can have NULL as base argument when
the nmemb argument is 0.  The base argument is required to be pointing to the
initial member of an array of nmemb objects.  It is not specified what
constitutes a valid pointer to an array of 0 objects, but glibc declares base
with attribute non-null, so the NULL will trigger a sanitizer runtime error.

Fix this by only calling bsearch if nmemb != 0.

2019-02-12  Tom de Vries  <tdevries@suse.de>

PR libbacktrace/81983
* dwarf.c (dwarf_lookup_pc): Don't call bsearch if nmemb == 0.

From-SVN: r268796

libbacktrace/ChangeLog
libbacktrace/dwarf.c

index 9e0da6353e32db18bdb0c78f04844eda5f8bb14c..e29c41f78ed2cb5a357dd3bc97060ee898407d6a 100644 (file)
@@ -1,3 +1,8 @@
+2019-02-12  Tom de Vries  <tdevries@suse.de>
+
+       PR libbacktrace/81983
+       * dwarf.c (dwarf_lookup_pc): Don't call bsearch if nmemb == 0.
+
 2019-02-10  Tom de Vries  <tdevries@suse.de>
 
        * Makefile.am (BUILDTESTS): Add btest_lto.
index d7dacf3ef32e23381b97db77eb2fa7475f56fa97..f338489fe4496fa8c550722936eb0fb49b11bc7b 100644 (file)
@@ -2821,8 +2821,10 @@ dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
   *found = 1;
 
   /* Find an address range that includes PC.  */
-  entry = bsearch (&pc, ddata->addrs, ddata->addrs_count,
-                  sizeof (struct unit_addrs), unit_addrs_search);
+  entry = (ddata->addrs_count == 0
+          ? NULL
+          : bsearch (&pc, ddata->addrs, ddata->addrs_count,
+                     sizeof (struct unit_addrs), unit_addrs_search));
 
   if (entry == NULL)
     {