gdb: fix "passing NULL to memcpy" UBsan error in dwarf2/cooked-index.c

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
This commit is contained in:
Simon Marchi 2022-04-12 14:37:24 -04:00
parent 558802e4d1
commit a8b7a13911

View File

@ -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 ());
}
}