RFA: Fix Coverity errors reported in CTF code in binutils

Nick Clifton nickc@redhat.com
Wed Jul 22 11:01:11 GMT 2020


Hi Nick,

  The Coverity source code scanner has identified three CTF related
  problems in the binutils sources.  The first is a potential memory
  leak in readelf, should a ctf command line option be specified more
  than once.  The fix is quite simple:

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 2406304fe3..c3cbd2cb23 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -4827,12 +4827,15 @@ parse_args (struct dump_data *dumpdata, int argc, char ** argv)
 	  request_dump (dumpdata, CTF_DUMP);
 	  break;
 	case OPTION_CTF_SYMBOLS:
+	  free (dump_ctf_symtab_name);
 	  dump_ctf_symtab_name = strdup (optarg);
 	  break;
 	case OPTION_CTF_STRINGS:
+	  free (dump_ctf_strtab_name);
 	  dump_ctf_strtab_name = strdup (optarg);
 	  break;
 	case OPTION_CTF_PARENT:
+	  free (dump_ctf_parent_name);
 	  dump_ctf_parent_name = strdup (optarg);
 	  break;
 	case OPTION_DYN_SYMS:


  The second appears to be a copy-and-paste error, again in the readelf
  sources:
  
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 2406304fe3..c3cbd2cb23 100644
--- a/binutils/readelf.c
@@ -14296,7 +14299,7 @@ dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
       symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
       symsect.cts_data = symdata;
     }
-  if (dump_ctf_strtab_name && dump_ctf_symtab_name[0] != 0)
+  if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
     {
       if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
 	{


  Lastly there is a potential double call to close() on the same file
  descriptor.  This happens in ctf_arc_write() where a successful call
  to close() then falls through to a second call on the same fd!  My
  proposed patch is below, and it involves tidying up the code a little
  and renaming the goto labels:

diff --git a/libctf/ctf-archive.c b/libctf/ctf-archive.c
index ac13d6dd5e..c42f5f923f 100644
--- a/libctf/ctf-archive.c
+++ b/libctf/ctf-archive.c
@@ -228,23 +228,16 @@ ctf_arc_write (const char *file, ctf_file_t ** ctf_files, size_t ctf_file_cnt,
 
   err = ctf_arc_write_fd (fd, ctf_files, ctf_file_cnt, names, threshold);
   if (err)
-    goto err;
+    goto err_close;
 
   if ((err = close (fd)) < 0)
-    {
-      ctf_dprintf ("ctf_arc_write(): Cannot close after writing to archive: "
-		   "%s\n", strerror (errno));
-      goto err_close;
-    }
-
- err:
-  close (fd);
-  if (err < 0)
-    unlink (file);
-
-  return err;
+    ctf_dprintf ("ctf_arc_write(): Cannot close after writing to archive: "
+		 "%s\n", strerror (errno));
+  goto err;
 
  err_close:
+  close (fd);
+ err:
   if (err < 0)
     unlink (file);

  Please let me know if you are happy for me to commit these patches, or
  if you think that there is a better solution.

Cheers
  Nick
  



More information about the Binutils mailing list