public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: fix "passing NULL to memcpy" UBsan error in dwarf2/cooked-index.c
@ 2022-04-12 18:37 Simon Marchi
  2022-04-12 18:41 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2022-04-12 18:37 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Reading a simple file compiled with :

    $ gcc -DONE=1 -gdwarf-4 -g3  test.c
    $ gcc --version
    gcc (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0

I get:

    Reading symbols from /tmp/cwd/a.out...
    /home/smarchi/src/binutils-gdb/gdb/dwarf2/cooked-index.c:332:11: runtime error: null pointer passed as argument 2, which is declared to never be null

It looks like even if the size is 0 (the size of the `entries` vector is
0), we shouldn't be passing a NULL pointer to memcpy.  And
`entries.data ()` returns NULL.

Fix that by using std::vector::insert to insert the items of entries
into m_entries.  I haven't checked, but it should essentially compile
down to a memcpy, since the vector elements are trivially copyiable.

Change-Id: I75f1c901e9b522e42e89eb5936e2c70d68eb21e5
---
 gdb/dwarf2/cooked-index.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/gdb/dwarf2/cooked-index.c b/gdb/dwarf2/cooked-index.c
index 784c06ea04b..b66ef5a1c64 100644
--- a/gdb/dwarf2/cooked-index.c
+++ b/gdb/dwarf2/cooked-index.c
@@ -327,10 +327,8 @@ cooked_index_vector::finalize ()
 	m_entries = std::move (entries);
       else
 	{
-	  size_t old_size = m_entries.size ();
-	  m_entries.resize (m_entries.size () + entries.size ());
-	  memcpy (m_entries.data () + old_size,
-		  entries.data (), entries.size () * sizeof (entries[0]));
+	  m_entries.reserve (m_entries.size () + entries.size ());
+	  m_entries.insert (m_entries.end (), entries.begin (), entries.end ());
 	}
     }
 
-- 
2.35.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: fix "passing NULL to memcpy" UBsan error in dwarf2/cooked-index.c
  2022-04-12 18:37 [PATCH] gdb: fix "passing NULL to memcpy" UBsan error in dwarf2/cooked-index.c Simon Marchi
@ 2022-04-12 18:41 ` Tom Tromey
  2022-04-12 18:42   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2022-04-12 18:41 UTC (permalink / raw)
  To: Simon Marchi via Gdb-patches; +Cc: Simon Marchi

>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

Simon> Fix that by using std::vector::insert to insert the items of entries
Simon> into m_entries.  I haven't checked, but it should essentially compile
Simon> down to a memcpy, since the vector elements are trivially copyiable.

Thank you.  This looks good to me.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: fix "passing NULL to memcpy" UBsan error in dwarf2/cooked-index.c
  2022-04-12 18:41 ` Tom Tromey
@ 2022-04-12 18:42   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2022-04-12 18:42 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi via Gdb-patches

On 2022-04-12 14:41, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Simon> Fix that by using std::vector::insert to insert the items of entries
> Simon> into m_entries.  I haven't checked, but it should essentially compile
> Simon> down to a memcpy, since the vector elements are trivially copyiable.
> 
> Thank you.  This looks good to me.
> 
> Tom


Pushed, thanks.

Simon

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-12 18:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-12 18:37 [PATCH] gdb: fix "passing NULL to memcpy" UBsan error in dwarf2/cooked-index.c Simon Marchi
2022-04-12 18:41 ` Tom Tromey
2022-04-12 18:42   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).