[v2,01/12] Zero-initialize linux note sections

Message ID 20180810025210.6942-2-pedromfc@linux.ibm.com
State Superseded
Headers show
Series
  • GDB support for more powerpc registers on linux
Related show

Commit Message

Pedro Franco de Carvalho Aug. 10, 2018, 2:51 a.m.
This patches changes linux-tdep.c so that the buffer used to write
note sections when generating a core file is zero-initialized.  This
way, bytes that are not collected won't contain random
data (e.g. padding bytes).

gdb/ChangeLog:
YYYY-MM-DD  Pedro Franco de Carvalho  <pedromfc@linux.ibm.com>

	* linux-tdep.c (linux_collect_regset_section_cb): Use
	std::vector<char> instead of char * for buf.  Remove xfree.
---
 gdb/linux-tdep.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

-- 
2.13.6

Comments

Tom Tromey Aug. 10, 2018, 4:13 p.m. | #1
>>>>> "Pedro" == Pedro Franco de Carvalho <pedromfc@linux.ibm.com> writes:


Pedro> This patches changes linux-tdep.c so that the buffer used to write
Pedro> note sections when generating a core file is zero-initialized.  This
Pedro> way, bytes that are not collected won't contain random
Pedro> data (e.g. padding bytes).

Pedro> -  char *buf;
Pedro> +  std::vector<char> buf (size);

It might be nice for future developers if there were a comment here
explaining that this is intentionally zero-initialized.  Other spots in
gdb use gdb::def_vector and perhaps someone might think that would be a
worthwhile micro-optimization.

Tom
Pedro Franco de Carvalho Aug. 10, 2018, 6:11 p.m. | #2
Tom Tromey <tom@tromey.com> writes:

> It might be nice for future developers if there were a comment here

> explaining that this is intentionally zero-initialized.  Other spots in

> gdb use gdb::def_vector and perhaps someone might think that would be a

> worthwhile micro-optimization.

>

> Tom


Ok! I'll add the comment below.

Thanks!

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 3cfa2a5aa4..2bcd2390c5 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1583,7 +1583,9 @@ linux_collect_regset_section_cb (const char *sect_name, int size,
 				 const struct regset *regset,
 				 const char *human_name, void *cb_data)
 {
-  char *buf;
+  /* This is intentionally zero-initialized by using std::vector, so
+     that any padding bytes in the core file will show as 0.  */
+  std::vector<char> buf (size);
   struct linux_collect_regset_section_cb_data *data
     = (struct linux_collect_regset_section_cb_data *) cb_data;
 
@@ -1592,19 +1594,17 @@ linux_collect_regset_section_cb (const char *sect_name, int size,
 
   gdb_assert (regset && regset->collect_regset);
 
-  buf = (char *) xmalloc (size);
-  regset->collect_regset (regset, data->regcache, -1, buf, size);
+  regset->collect_regset (regset, data->regcache, -1, buf.data (), size);
 
   /* PRSTATUS still needs to be treated specially.  */
   if (strcmp (sect_name, ".reg") == 0)
     data->note_data = (char *) elfcore_write_prstatus
       (data->obfd, data->note_data, data->note_size, data->lwp,
-       gdb_signal_to_host (data->stop_signal), buf);
+       gdb_signal_to_host (data->stop_signal), buf.data ());
   else
     data->note_data = (char *) elfcore_write_register_note
       (data->obfd, data->note_data, data->note_size,
-       sect_name, buf, size);
-  xfree (buf);
+       sect_name, buf.data (), size);
 
   if (data->note_data == NULL)
     data->abort_iteration = 1;
Tom Tromey Aug. 10, 2018, 8:50 p.m. | #3
>>>>> "Pedro" == Pedro Franco de Carvalho <pedromfc@linux.ibm.com> writes:


Pedro> Ok! I'll add the comment below.

Thank you.

Tom

Patch

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 3cfa2a5aa4..f4edab33ea 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1583,7 +1583,7 @@  linux_collect_regset_section_cb (const char *sect_name, int size,
 				 const struct regset *regset,
 				 const char *human_name, void *cb_data)
 {
-  char *buf;
+  std::vector<char> buf (size);
   struct linux_collect_regset_section_cb_data *data
     = (struct linux_collect_regset_section_cb_data *) cb_data;
 
@@ -1592,19 +1592,17 @@  linux_collect_regset_section_cb (const char *sect_name, int size,
 
   gdb_assert (regset && regset->collect_regset);
 
-  buf = (char *) xmalloc (size);
-  regset->collect_regset (regset, data->regcache, -1, buf, size);
+  regset->collect_regset (regset, data->regcache, -1, buf.data (), size);
 
   /* PRSTATUS still needs to be treated specially.  */
   if (strcmp (sect_name, ".reg") == 0)
     data->note_data = (char *) elfcore_write_prstatus
       (data->obfd, data->note_data, data->note_size, data->lwp,
-       gdb_signal_to_host (data->stop_signal), buf);
+       gdb_signal_to_host (data->stop_signal), buf.data ());
   else
     data->note_data = (char *) elfcore_write_register_note
       (data->obfd, data->note_data, data->note_size,
-       sect_name, buf, size);
-  xfree (buf);
+       sect_name, buf.data (), size);
 
   if (data->note_data == NULL)
     data->abort_iteration = 1;