b6c903
commit e3217c7fd9e67aa2d53700bb1da9a966e73b9684
b6c903
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
b6c903
Date:   Thu Jun 10 00:41:35 2021 +0530
b6c903
b6c903
    iconv: Remove alloca use in gconv-modules configuration parsing
b6c903
    
b6c903
    The alloca sizes ought to be constrained to PATH_MAX, but replace them
b6c903
    with dynamic allocation to be safe.  A static PATH_MAX array would
b6c903
    have worked too but Hurd does not have PATH_MAX and the code path is
b6c903
    not hot enough to micro-optimise this allocation.  Revisit if any of
b6c903
    those realities change.
b6c903
    
b6c903
    Reviewed-by: DJ Delorie <dj@redhat.com>
b6c903
b6c903
diff --git a/iconv/gconv_conf.c b/iconv/gconv_conf.c
b6c903
index 8eb981fca7cee36a..3099bf192adce711 100644
b6c903
--- a/iconv/gconv_conf.c
b6c903
+++ b/iconv/gconv_conf.c
b6c903
@@ -557,15 +557,15 @@ __gconv_read_conf (void)
b6c903
 
b6c903
   for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
b6c903
     {
b6c903
-#define BUF_LEN elem_len + sizeof (gconv_conf_dirname)
b6c903
-
b6c903
       const char *elem = __gconv_path_elem[cnt].name;
b6c903
       size_t elem_len = __gconv_path_elem[cnt].len;
b6c903
-      char *buf;
b6c903
 
b6c903
       /* No slash needs to be inserted between elem and gconv_conf_filename;
b6c903
 	 elem already ends in a slash.  */
b6c903
-      buf = alloca (BUF_LEN);
b6c903
+      char *buf = malloc (elem_len + sizeof (gconv_conf_dirname));
b6c903
+      if (buf == NULL)
b6c903
+	continue;
b6c903
+
b6c903
       char *cp = __mempcpy (__mempcpy (buf, elem, elem_len),
b6c903
 			    gconv_conf_filename, sizeof (gconv_conf_filename));
b6c903
 
b6c903
@@ -594,15 +594,16 @@ __gconv_read_conf (void)
b6c903
 	      if (len > strlen (suffix)
b6c903
 		  && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
b6c903
 		{
b6c903
-		  /* LEN <= PATH_MAX so this alloca is not unbounded.  */
b6c903
-		  char *conf = alloca (BUF_LEN + len + 1);
b6c903
-		  cp = stpcpy (conf, buf);
b6c903
-		  sprintf (cp, "/%s", ent->d_name);
b6c903
+		  char *conf;
b6c903
+		  if (__asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
b6c903
+		    continue;
b6c903
 		  read_conf_file (conf, elem, elem_len, &modules, &nmodules);
b6c903
+		  free (conf);
b6c903
 		}
b6c903
 	    }
b6c903
 	  __closedir (confdir);
b6c903
 	}
b6c903
+      free (buf);
b6c903
     }
b6c903
 #endif
b6c903
 
b6c903
diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c
b6c903
index fafc686ae25fb5c1..2f9d5f45ad3a8159 100644
b6c903
--- a/iconv/iconvconfig.c
b6c903
+++ b/iconv/iconvconfig.c
b6c903
@@ -712,7 +712,6 @@ handle_file (const char *dir, const char *infile)
b6c903
 static int
b6c903
 handle_dir (const char *dir)
b6c903
 {
b6c903
-#define BUF_LEN prefix_len + dirlen + sizeof "gconv-modules.d"
b6c903
   char *cp;
b6c903
   size_t dirlen = strlen (dir);
b6c903
   bool found = false;
b6c903
@@ -726,7 +725,10 @@ handle_dir (const char *dir)
b6c903
     }
b6c903
 
b6c903
   /* First, look for a gconv-modules file.  */
b6c903
-  char buf[BUF_LEN];
b6c903
+  char *buf = malloc (prefix_len + dirlen + sizeof "gconv-modules.d");
b6c903
+  if (buf == NULL)
b6c903
+    goto out;
b6c903
+
b6c903
   cp = buf;
b6c903
   if (dir[0] == '/')
b6c903
     cp = mempcpy (cp, prefix, prefix_len);
b6c903
@@ -756,16 +758,19 @@ handle_dir (const char *dir)
b6c903
 	  if (len > strlen (suffix)
b6c903
 	      && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
b6c903
 	    {
b6c903
-	      /* LEN <= PATH_MAX so this alloca is not unbounded.  */
b6c903
-	      char *conf = alloca (BUF_LEN + len + 1);
b6c903
-	      cp = stpcpy (conf, buf);
b6c903
-	      sprintf (cp, "/%s", ent->d_name);
b6c903
+	      char *conf;
b6c903
+	      if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
b6c903
+		continue;
b6c903
 	      found |= handle_file (dir, conf);
b6c903
+	      free (conf);
b6c903
 	    }
b6c903
 	}
b6c903
       closedir (confdir);
b6c903
     }
b6c903
 
b6c903
+  free (buf);
b6c903
+
b6c903
+out:
b6c903
   if (!found)
b6c903
     {
b6c903
       error (0, errno, "failed to open gconv configuration files in `%s'",