public inbox for dwz@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: dwz@sourceware.org, jakub@redhat.com, mark@klomp.org
Subject: [committed] Factor out dwz_files_1
Date: Tue, 23 Mar 2021 11:26:35 +0100	[thread overview]
Message-ID: <20210323102633.GA13952@delia> (raw)

Hi,

In dwz_files, a variable resa is malloced at the start, and freed at the end.
This prevents (early) returns, which makes the code more complicated than
necessary.  Also, the function already contains an early return, which means
it leaks resa.

Fix this by factoring out dwz_files_1 out of dwz_files, where dwz_files is a
wrapper tasked with alloc/free of variable resa.

Committed to trunk.

Thanks,
- Tom

Factor out dwz_files_1

2021-03-23  Tom de Vries  <tdevries@suse.de>

	* dwz.c (dwz_files_1): New function, factored out of ...
	(dwz_files): ... here.

---
 dwz.c | 111 +++++++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 62 insertions(+), 49 deletions(-)

diff --git a/dwz.c b/dwz.c
index a7aa23b..4fc01a3 100644
--- a/dwz.c
+++ b/dwz.c
@@ -16285,18 +16285,15 @@ dwz_one_file (const char *file, const char *outfile)
 /* Dwarf-compress FILES.  If HARDLINK, detect if some files are hardlinks and
    if so, dwarf-compress just one, and relink the others.  */
 static int
-dwz_files (int nr_files, char *files[], bool hardlink)
+dwz_files_1 (int nr_files, char *files[], bool hardlink,
+	     struct file_result *resa)
 {
   int ret = 0;
   int i;
   const char *file;
-  struct file_result *resa;
   bool hardlinks = false;
   int successcount = 0;
 
-  resa = (struct file_result *) malloc ((nr_files) * sizeof (*resa));
-  if (resa == NULL)
-    error (1, ENOMEM, "failed to allocate result array");
   for (i = 0; i < nr_files; ++i)
     resa[i].die_count = 0;
 
@@ -16344,65 +16341,81 @@ dwz_files (int nr_files, char *files[], bool hardlink)
 	hardlinks = true;
     }
 
-  if (multifile && successcount < 2)
+  if (multifile == NULL)
+    return ret;
+
+  if (successcount < 2)
     {
       error (0, 0, "Too few files for multifile optimization");
-      multifile = NULL;
+      return ret;
     }
 
-  if (multifile
-      && multi_info_off == 0 && multi_str_off == 0 && multi_macro_off == 0)
+  if (multi_info_off == 0 && multi_str_off == 0 && multi_macro_off == 0)
     {
       if (!quiet)
 	error (0, 0, "No suitable DWARF found for multifile optimization");
-      multifile = NULL;
+      return ret;
     }
 
-  if (multifile)
+  unsigned int multifile_die_count = 0;
+  int multi_fd = optimize_multifile (&multifile_die_count);
+  DSO *dso;
+  if (multi_fd == -1)
+    return 1;
+
+  dso = read_multifile (multi_fd, multifile_die_count);
+  if (dso == NULL)
+    ret = 1;
+  else
     {
-      unsigned int multifile_die_count = 0;
-      int multi_fd = optimize_multifile (&multifile_die_count);
-      DSO *dso;
-      if (multi_fd == -1)
-	return 1;
-      dso = read_multifile (multi_fd, multifile_die_count);
-      if (dso == NULL)
-	ret = 1;
-      else
+      for (i = 0; i < nr_files; i++)
 	{
-	  for (i = 0; i < nr_files; i++)
-	    {
-	      dw_cu_ref cu;
-	      file = files[i];
-	      if (stats_p)
-		init_stats (file);
-	      multifile_mode = MULTIFILE_MODE_FI;
-	      /* Don't process again files that couldn't
-		 be processed successfully.  */
-	      if (resa[i].res == -1)
-		continue;
-	      for (cu = alt_first_cu; cu; cu = cu->cu_next)
-		alt_clear_dups (cu->cu_die);
-	      ret |= dwz (file, NULL, &resa[i],
-			  hardlinks ? resa : NULL, files);
-	    }
-	  elf_end (dso->elf);
-	  close (multi_fd);
-	  free (dso);
+	  dw_cu_ref cu;
+	  file = files[i];
+	  if (stats_p)
+	    init_stats (file);
+	  multifile_mode = MULTIFILE_MODE_FI;
+	  /* Don't process again files that couldn't
+	     be processed successfully.  */
+	  if (resa[i].res == -1)
+	    continue;
+	  for (cu = alt_first_cu; cu; cu = cu->cu_next)
+	    alt_clear_dups (cu->cu_die);
+	  ret |= dwz (file, NULL, &resa[i],
+		      hardlinks ? resa : NULL, files);
 	}
-      cleanup ();
-      strp_htab = alt_strp_htab;
-      off_htab = alt_off_htab;
-      dup_htab = alt_dup_htab;
-      macro_htab = alt_macro_htab;
-      pool = alt_pool;
-      ob = alt_ob;
-      ob2 = alt_ob2;
-      cleanup ();
+      elf_end (dso->elf);
+      close (multi_fd);
+      free (dso);
     }
 
-  free (resa);
+  cleanup ();
+
+  strp_htab = alt_strp_htab;
+  off_htab = alt_off_htab;
+  dup_htab = alt_dup_htab;
+  macro_htab = alt_macro_htab;
+  pool = alt_pool;
+  ob = alt_ob;
+  ob2 = alt_ob2;
+  cleanup ();
+
+  return ret;
+}
 
+/* Wrapper around dwz_files_1 that takes care of malloc and free of resa.  */
+static int
+dwz_files (int nr_files, char *files[], bool hardlink)
+{
+  int ret;
+  struct file_result *resa
+    = (struct file_result *) malloc ((nr_files) * sizeof (*resa));
+  if (resa == NULL)
+    error (1, ENOMEM, "failed to allocate result array");
+
+  ret = dwz_files_1 (nr_files, files, hardlink, resa);
+
+  free (resa);
   return ret;
 }
 

                 reply	other threads:[~2021-03-23 10:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210323102633.GA13952@delia \
    --to=tdevries@suse.de \
    --cc=dwz@sourceware.org \
    --cc=jakub@redhat.com \
    --cc=mark@klomp.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).