This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[COMMITTED] Fix some (harmless) cppcheck warnings.


[dwarf_getaranges.c:149]: (warning) Ineffective statement similar to '*A++;'.
Did you intend to write '(*A)++;'?

There was already an XXX statement that we weren't using the result.
Explicitly read the segment_size and check it is zero. And report an
error if it isn't, since we aren't prepared to handle such a case.

[arlib.c:62]: (error) Uninitialized variable: tmpbuf
[arlib.c:124]: (error) Uninitialized variable: tmpbuf

cppcheck is wrong. tmpbuf is initialized in the snprintf call whose result
is use in the same memcpy call. It does make the code less readable and
harder to understand. So explicitly split the snprintf and memcpy calls.

[nm.c:766]: (warning) Assert statement modifies 'cnt'.

The cnt variable was only used for this assert sanity check.
But it is bad style to do have side effects inside an assert statement.
Increase cnt after the assert.

Reported-by: David Binderman <dcb314@hotmail.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 libdw/ChangeLog          |  5 +++++
 libdw/dwarf_getaranges.c |  7 ++++---
 src/ChangeLog            |  7 +++++++
 src/arlib.c              | 12 ++++++------
 src/nm.c                 |  3 ++-
 5 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index c355c30..dc0c4c9 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,8 @@
+2013-11-09  Mark Wielaard  <mjw@redhat.com>
+
+	* dwarf_getaranges.c (dwarf_getaranges): Read segment_size and
+	check that it is zero.
+
 2013-11-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
 	* cfi.h (struct Dwarf_Frame_s): Make the comment more specific.
diff --git a/libdw/dwarf_getaranges.c b/libdw/dwarf_getaranges.c
index cc70cb2..20ac7ec 100644
--- a/libdw/dwarf_getaranges.c
+++ b/libdw/dwarf_getaranges.c
@@ -144,9 +144,10 @@ dwarf_getaranges (dbg, aranges, naranges)
       if (address_size != 4 && address_size != 8)
 	goto invalid;
 
-      /* Ignore the segment size value.  */
-      // XXX Really?
-      (void) *readp++;
+      /* We don't actually support segment selectors.  */
+      unsigned int segment_size = *readp++;
+      if (segment_size != 0)
+	goto invalid;
 
       /* Round the address to the next multiple of 2*address_size.  */
       readp += ((2 * address_size - ((readp - hdrstart) % (2 * address_size)))
diff --git a/src/ChangeLog b/src/ChangeLog
index aebcb2f..94bc27a 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2013-11-09  Mark Wielaard  <mjw@redhat.com>
+
+	* arlib.c (arlib_init): Call snprintf before using the result
+	with memcpy.
+	(arlib_finalize): Likewise.
+	* nm.c (show_symbols_sysv): Don't modify cnt inside assert.
+
 2013-11-07  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
 	* Makefile.am (bin_PROGRAMS): Add stack.
diff --git a/src/arlib.c b/src/arlib.c
index 62c517c..43a9145 100644
--- a/src/arlib.c
+++ b/src/arlib.c
@@ -59,11 +59,11 @@ arlib_init (void)
      _FORTIFY_SOURCE=2 would not let us play these games.  Therefore
      we play it safe.  */
   char tmpbuf[sizeof (ar_hdr.ar_date) + 1];
-  memcpy (ar_hdr.ar_date, tmpbuf,
-	  snprintf (tmpbuf, sizeof (tmpbuf), "%-*lld",
+  int s = snprintf (tmpbuf, sizeof (tmpbuf), "%-*lld",
 		    (int) sizeof (ar_hdr.ar_date),
                     (arlib_deterministic_output ? 0
-                     : (long long int) time (NULL))));
+                     : (long long int) time (NULL)));
+  memcpy (ar_hdr.ar_date, tmpbuf, s);
   assert ((sizeof (struct ar_hdr)  % sizeof (uint32_t)) == 0);
 
   /* Note the string for the ar_uid and ar_gid cases is longer than
@@ -121,10 +121,10 @@ arlib_finalize (void)
 
       symtab.longnames = obstack_finish (&symtab.longnamesob);
 
-      memcpy (&((struct ar_hdr *) symtab.longnames)->ar_size, tmpbuf,
-	      snprintf (tmpbuf, sizeof (tmpbuf), "%-*zu",
+      int s = snprintf (tmpbuf, sizeof (tmpbuf), "%-*zu",
 			(int) sizeof (((struct ar_hdr *) NULL)->ar_size),
-			symtab.longnameslen - sizeof (struct ar_hdr)));
+			symtab.longnameslen - sizeof (struct ar_hdr));
+      memcpy (&((struct ar_hdr *) symtab.longnames)->ar_size, tmpbuf, s);
     }
 
   symtab.symsofflen = obstack_object_size (&symtab.symsoffob);
diff --git a/src/nm.c b/src/nm.c
index 7aae84b..d434f44 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -763,7 +763,8 @@ show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname,
     {
       GElf_Shdr shdr_mem;
 
-      assert (elf_ndxscn (scn) == cnt++);
+      assert (elf_ndxscn (scn) == cnt);
+      cnt++;
 
       char *name = elf_strptr (ebl->elf, shstrndx,
 			       gelf_getshdr (scn, &shdr_mem)->sh_name);
-- 
1.8.3.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]