This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[PATCH 1/3] Do not pass NULL to memcpy
- From: Tom Tromey <tom at tromey dot com>
- To: gdb-patches at sourceware dot org
- Cc: Tom Tromey <tom at tromey dot com>
- Date: Fri, 27 Jul 2018 11:25:58 -0600
- Subject: [PATCH 1/3] Do not pass NULL to memcpy
- References: <20180727172600.15994-1-tom@tromey.com>
I built gdb with -fsanitize=undefined and ran the test suite. This
patch fixes a couple of complaints that occur when passing NULL to
memcpy, which is undefined behavior according to the C standard.
gdb/ChangeLog
2018-07-27 Tom Tromey <tom@tromey.com>
* namespace.c (add_using_directive): Don't pass NULL to memcpy.
* dwarf2-frame.h (struct dwarf2_frame_state_reg_info): Don't pass
NULL to memcpy.
---
gdb/ChangeLog | 6 ++++++
gdb/dwarf2-frame.h | 3 ++-
gdb/namespace.c | 5 +++--
3 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/gdb/dwarf2-frame.h b/gdb/dwarf2-frame.h
index 52316e5e168..6844010c8df 100644
--- a/gdb/dwarf2-frame.h
+++ b/gdb/dwarf2-frame.h
@@ -110,7 +110,8 @@ struct dwarf2_frame_state_reg_info
size_t size = src.num_regs * sizeof (struct dwarf2_frame_state_reg);
reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
- memcpy (reg, src.reg, size);
+ if (size > 0)
+ memcpy (reg, src.reg, size);
}
/* Assignment operator for both move-assignment and copy-assignment. */
diff --git a/gdb/namespace.c b/gdb/namespace.c
index be998d9d491..85c0c4b14d7 100644
--- a/gdb/namespace.c
+++ b/gdb/namespace.c
@@ -111,8 +111,9 @@ add_using_directive (struct using_direct **using_directives,
else
newobj->declaration = declaration;
- memcpy (newobj->excludes, excludes.data (),
- excludes.size () * sizeof (*newobj->excludes));
+ if (!excludes.empty ())
+ memcpy (newobj->excludes, excludes.data (),
+ excludes.size () * sizeof (*newobj->excludes));
newobj->excludes[excludes.size ()] = NULL;
newobj->next = *using_directives;
--
2.13.6