179894
commit 9429049c178b3af3d6afeb3717ff1f2214dc9572
179894
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
179894
Date:   Mon Jun 28 09:15:55 2021 +0530
179894
179894
    iconvconfig: Fix multiple issues
179894
    
179894
    It was noticed on big-endian systems that msgfmt would fail with the
179894
    following error:
179894
    
179894
    msgfmt: gconv_builtin.c:70: __gconv_get_builtin_trans: Assertion `cnt < sizeof (map) / sizeof (map[0])' failed.
179894
    Aborted (core dumped)
179894
    
179894
    This is only seen on installed systems because it was due to a
179894
    corrupted gconv-modules.cache.  iconvconfig had the following issues
179894
    (it was specifically freeing fulldir that caused this issue, but other
179894
    cleanups are also needed) that this patch fixes.
179894
    
179894
    - Add prefix only if dir starts with '/'
179894
    - Use asprintf instead of mempcpy so that the directory string is NULL
179894
      terminated
179894
    - Make a copy of the directory reference in new_module so that fulldir
179894
      can be freed within the same scope in handle_dir.
179894
    
179894
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
179894
179894
diff --git a/iconv/Makefile b/iconv/Makefile
179894
index d09b8ac842731780..6df9862e748ae588 100644
179894
--- a/iconv/Makefile
179894
+++ b/iconv/Makefile
179894
@@ -33,7 +33,7 @@ vpath %.c ../locale/programs ../intl
179894
 iconv_prog-modules = iconv_charmap charmap charmap-dir linereader \
179894
 		     dummy-repertoire simple-hash xstrdup xmalloc \
179894
 		     record-status
179894
-iconvconfig-modules = strtab xmalloc hash-string
179894
+iconvconfig-modules = strtab xmalloc xasprintf xstrdup hash-string
179894
 extra-objs	   = $(iconv_prog-modules:=.o) $(iconvconfig-modules:=.o)
179894
 CFLAGS-iconv_prog.c += -I../locale/programs
179894
 CFLAGS-iconv_charmap.c += -I../locale/programs
179894
diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c
179894
index 01ecf6f67d55dbbf..777da870d2f8e99a 100644
179894
--- a/iconv/iconvconfig.c
179894
+++ b/iconv/iconvconfig.c
179894
@@ -250,6 +250,7 @@ static const char gconv_module_ext[] = MODULE_EXT;
179894
 
179894
 
179894
 #include <programs/xmalloc.h>
179894
+#include <programs/xasprintf.h>
179894
 
179894
 
179894
 /* C string table handling.  */
179894
@@ -519,11 +520,12 @@ module_compare (const void *p1, const void *p2)
179894
 /* Create new module record.  */
179894
 static void
179894
 new_module (const char *fromname, size_t fromlen, const char *toname,
179894
-	    size_t tolen, const char *directory,
179894
+	    size_t tolen, const char *dir_in,
179894
 	    const char *filename, size_t filelen, int cost, size_t need_ext)
179894
 {
179894
   struct module *new_module;
179894
-  size_t dirlen = strlen (directory) + 1;
179894
+  size_t dirlen = strlen (dir_in) + 1;
179894
+  const char *directory = xstrdup (dir_in);
179894
   char *tmp;
179894
   void **inserted;
179894
 
179894
@@ -654,20 +656,10 @@ handle_dir (const char *dir)
179894
   size_t dirlen = strlen (dir);
179894
   bool found = false;
179894
 
179894
-  /* Add the prefix before sending it off to the parser.  */
179894
-  char *fulldir = xmalloc (prefix_len + dirlen + 2);
179894
-  char *cp = mempcpy (mempcpy (fulldir, prefix, prefix_len), dir, dirlen);
179894
+  char *fulldir = xasprintf ("%s%s%s", dir[0] == '/' ? prefix : "",
179894
+			     dir, dir[dirlen - 1] != '/' ? "/" : "");
179894
 
179894
-  if (dir[dirlen - 1] != '/')
179894
-    {
179894
-      *cp++ = '/';
179894
-      *cp = '\0';
179894
-      dirlen++;
179894
-    }
179894
-
179894
-  found = gconv_parseconfdir (fulldir, dirlen + prefix_len);
179894
-
179894
-  free (fulldir);
179894
+  found = gconv_parseconfdir (fulldir, strlen (fulldir));
179894
 
179894
   if (!found)
179894
     {
179894
@@ -679,6 +671,8 @@ handle_dir (const char *dir)
179894
 	     "configuration files with names ending in .conf.");
179894
     }
179894
 
179894
+  free (fulldir);
179894
+
179894
   return found ? 0 : 1;
179894
 }
179894