From af62665e1339d970ab8ea3e3260dbdbde0009c2d Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 31 Mar 2020 07:29:53 -0600 Subject: Don't pass NULL to memcpy in gdb I compiled gdb with -fsanitize=undefined and ran the test suite. A couple of reports came from passing NULL to memcpy, e.g.: [...]btrace-common.cc:176:13: runtime error: null pointer passed as argument 2, which is declared to never be null While it would be better to fix this in the standard, in the meantime it seems easy to avoid this error. gdb/ChangeLog 2020-03-31 Tom Tromey * dwarf2/abbrev.c (abbrev_table::read): Conditionally call memcpy. gdbsupport/ChangeLog 2020-03-31 Tom Tromey * btrace-common.cc (btrace_data_append): Conditionally call memcpy. --- gdb/ChangeLog | 5 +++++ gdb/dwarf2/abbrev.c | 5 +++-- gdbsupport/ChangeLog | 5 +++++ gdbsupport/btrace-common.cc | 3 ++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 67aa872..d3873da 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2020-03-31 Tom Tromey + + * dwarf2/abbrev.c (abbrev_table::read): Conditionally call + memcpy. + 2020-03-30 Nelson Chu * features/riscv/32bit-csr.xml: Regenerated. diff --git a/gdb/dwarf2/abbrev.c b/gdb/dwarf2/abbrev.c index 59ff138..b850180 100644 --- a/gdb/dwarf2/abbrev.c +++ b/gdb/dwarf2/abbrev.c @@ -168,8 +168,9 @@ abbrev_table::read (struct objfile *objfile, cur_abbrev->attrs = XOBNEWVEC (&abbrev_table->m_abbrev_obstack, struct attr_abbrev, cur_abbrev->num_attrs); - memcpy (cur_abbrev->attrs, cur_attrs.data (), - cur_abbrev->num_attrs * sizeof (struct attr_abbrev)); + if (!cur_attrs.empty ()) + memcpy (cur_abbrev->attrs, cur_attrs.data (), + cur_abbrev->num_attrs * sizeof (struct attr_abbrev)); abbrev_table->add_abbrev (abbrev_number, cur_abbrev); diff --git a/gdbsupport/ChangeLog b/gdbsupport/ChangeLog index 1d27971..86233e8 100644 --- a/gdbsupport/ChangeLog +++ b/gdbsupport/ChangeLog @@ -1,3 +1,8 @@ +2020-03-31 Tom Tromey + + * btrace-common.cc (btrace_data_append): Conditionally call + memcpy. + 2020-03-27 Andrew Burgess * create-version.sh: Resolve issues highlighted by shellcheck. diff --git a/gdbsupport/btrace-common.cc b/gdbsupport/btrace-common.cc index 7d4f642..e8b24db 100644 --- a/gdbsupport/btrace-common.cc +++ b/gdbsupport/btrace-common.cc @@ -173,7 +173,8 @@ btrace_data_append (struct btrace_data *dst, size = src->variant.pt.size + dst->variant.pt.size; data = (gdb_byte *) xmalloc (size); - memcpy (data, dst->variant.pt.data, dst->variant.pt.size); + if (dst->variant.pt.size > 0) + memcpy (data, dst->variant.pt.data, dst->variant.pt.size); memcpy (data + dst->variant.pt.size, src->variant.pt.data, src->variant.pt.size); -- cgit v1.1