diff mbox series

[libbacktrace] Handle bsearch with NULL base in dwarf_lookup_pc

Message ID 20190212083649.GA14912@delia
State New
Headers show
Series [libbacktrace] Handle bsearch with NULL base in dwarf_lookup_pc | expand

Commit Message

Tom de Vries Feb. 12, 2019, 8:36 a.m. UTC
Hi,

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.

OK for trunk?

Thanks,
- Tom

[libbacktrace] Handle bsearch with NULL base in dwarf_lookup_pc

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

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

---
 libbacktrace/dwarf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Li, Pan2 via Gcc-patches Feb. 12, 2019, 1:54 p.m. UTC | #1
On Tue, Feb 12, 2019 at 12:36 AM Tom de Vries <tdevries@suse.de> wrote:
>
> 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.
>
> OK for trunk?
>
> Thanks,
> - Tom
>
> [libbacktrace] Handle bsearch with NULL base in dwarf_lookup_pc
>
> 2019-02-12  Tom de Vries  <tdevries@suse.de>
>
>         PR libbacktrace/81983
>         * dwarf.c (dwarf_lookup_pc): Don't call bsearch if nmemb == 0.

This is OK.

Thanks.

Ian
diff mbox series

Patch

diff --git a/libbacktrace/dwarf.c b/libbacktrace/dwarf.c
index d7dacf3ef32..f338489fe44 100644
--- a/libbacktrace/dwarf.c
+++ b/libbacktrace/dwarf.c
@@ -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)
     {