public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Don't pass NULL to memcpy in gdb
@ 2020-03-20 15:01 Tom Tromey
  2020-03-21  2:51 ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2020-03-20 15:01 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

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-20  Tom Tromey  <tromey@adacore.com>

	* dwarf2/abbrev.c (abbrev_table::read): Conditionally call
	memcpy.

gdbsupport/ChangeLog
2020-03-20  Tom Tromey  <tromey@adacore.com>

	* 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/dwarf2/abbrev.c b/gdb/dwarf2/abbrev.c
index 59ff138b33d..b85018060fa 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/btrace-common.cc b/gdbsupport/btrace-common.cc
index 7d4f6424c82..e8b24db7d53 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);
 
-- 
2.21.1


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

* Re: [PATCH] Don't pass NULL to memcpy in gdb
  2020-03-20 15:01 [PATCH] Don't pass NULL to memcpy in gdb Tom Tromey
@ 2020-03-21  2:51 ` Simon Marchi
  2020-03-22 19:04   ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2020-03-21  2:51 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 2020-03-20 11:01 a.m., Tom Tromey wrote:
> 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.

I hope you're joking here :).  But otherwise, this LGTM.

Simon

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

* Re: [PATCH] Don't pass NULL to memcpy in gdb
  2020-03-21  2:51 ` Simon Marchi
@ 2020-03-22 19:04   ` Tom Tromey
  0 siblings, 0 replies; 3+ messages in thread
From: Tom Tromey @ 2020-03-22 19:04 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

>> While it would be better to fix this in the standard, in the meantime
>> it seems easy to avoid this error.

Simon> I hope you're joking here :).  But otherwise, this LGTM.

I actually do think it would be better for the standard to change to say
that memcpy should accept NULL pointers provided that the length is 0.
Probably most implementations already work this way and it seems
harmless to allow.  It also seems relatively unlikely to change though.

Tom

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

end of thread, other threads:[~2020-03-22 19:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-20 15:01 [PATCH] Don't pass NULL to memcpy in gdb Tom Tromey
2020-03-21  2:51 ` Simon Marchi
2020-03-22 19:04   ` Tom Tromey

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).