446cf2
commit 5e598c2bbf938eac0f4045f5143f9dd723646672
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Fri Oct 30 18:40:28 2020 +0100
446cf2
446cf2
    elf: In ldconfig, extract the new_sub_entry function from search_dir
446cf2
    
446cf2
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
446cf2
446cf2
diff --git a/elf/ldconfig.c b/elf/ldconfig.c
446cf2
index 7c8fd29387463a8a..be730ceb075f6c1f 100644
446cf2
--- a/elf/ldconfig.c
446cf2
+++ b/elf/ldconfig.c
446cf2
@@ -328,6 +328,23 @@ warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
446cf2
 	   "Andreas Jaeger");
446cf2
 }
446cf2
 
446cf2
+/* Allocate a new subdirectory with full path PATH under ENTRY, using
446cf2
+   inode data from *ST.  */
446cf2
+static struct dir_entry *
446cf2
+new_sub_entry (const struct dir_entry *entry, const char *path,
446cf2
+	       const struct stat64 *st)
446cf2
+{
446cf2
+  struct dir_entry *new_entry = xmalloc (sizeof (struct dir_entry));
446cf2
+  new_entry->from_file = entry->from_file;
446cf2
+  new_entry->from_line = entry->from_line;
446cf2
+  new_entry->path = xstrdup (path);
446cf2
+  new_entry->flag = entry->flag;
446cf2
+  new_entry->next = NULL;
446cf2
+  new_entry->ino = st->st_ino;
446cf2
+  new_entry->dev = st->st_dev;
446cf2
+  return new_entry;
446cf2
+}
446cf2
+
446cf2
 /* Add a single directory entry.  */
446cf2
 static void
446cf2
 add_single_dir (struct dir_entry *entry, int verbose)
446cf2
@@ -823,26 +840,17 @@ search_dir (const struct dir_entry *entry)
446cf2
 
446cf2
       if (is_dir && is_hwcap_platform (direntry->d_name))
446cf2
 	{
446cf2
-	  /* Handle subdirectory later.  */
446cf2
-	  struct dir_entry *new_entry;
446cf2
-
446cf2
-	  new_entry = xmalloc (sizeof (struct dir_entry));
446cf2
-	  new_entry->from_file = entry->from_file;
446cf2
-	  new_entry->from_line = entry->from_line;
446cf2
-	  new_entry->path = xstrdup (file_name);
446cf2
-	  new_entry->flag = entry->flag;
446cf2
-	  new_entry->next = NULL;
446cf2
 	  if (!is_link
446cf2
 	      && direntry->d_type != DT_UNKNOWN
446cf2
 	      && __builtin_expect (lstat64 (real_file_name, &lstat_buf), 0))
446cf2
 	    {
446cf2
 	      error (0, errno, _("Cannot lstat %s"), file_name);
446cf2
-	      free (new_entry->path);
446cf2
-	      free (new_entry);
446cf2
 	      continue;
446cf2
 	    }
446cf2
-	  new_entry->ino = lstat_buf.st_ino;
446cf2
-	  new_entry->dev = lstat_buf.st_dev;
446cf2
+
446cf2
+	  /* Handle subdirectory later.  */
446cf2
+	  struct dir_entry *new_entry = new_sub_entry (entry, file_name,
446cf2
+						       &lstat_buf);
446cf2
 	  add_single_dir (new_entry, 0);
446cf2
 	  continue;
446cf2
 	}