740027
From e16ae8b0f8a16f05d5881ad282bd58b31645a34f Mon Sep 17 00:00:00 2001
740027
From: Kamil Dudka <kdudka@redhat.com>
740027
Date: Fri, 15 Jun 2018 12:47:38 +0200
740027
Subject: [PATCH] Resolves: #1259942 - fix memory leak in sort/I18N
740027
MIME-Version: 1.0
740027
Content-Type: text/plain; charset=UTF-8
740027
Content-Transfer-Encoding: 8bit
740027
740027
Patches written by Pádraig.
740027
740027
Note that the corresponding i18n/sort-month test was not included
740027
because it breaks unless sort is compiled -Dlint and we do not want
740027
to decrease performance of the resulting RPMs (and valgrind is not
740027
installed in production buildroots anyway).
740027
---
740027
 src/sort.c | 37 ++++++++++++++++++++++---------------
740027
 1 file changed, 22 insertions(+), 15 deletions(-)
740027
740027
diff --git a/src/sort.c b/src/sort.c
740027
index e47b039..c04b513 100644
740027
--- a/src/sort.c
740027
+++ b/src/sort.c
740027
@@ -2861,8 +2861,8 @@ getmonth_mb (const char *s, size_t len, char **ea)
740027
   register int lo = 0, hi = MONTHS_PER_YEAR, result;
740027
   char *tmp;
740027
   size_t wclength, mblength;
740027
-  const char **pp;
740027
-  const wchar_t **wpp;
740027
+  const char *pp;
740027
+  const wchar_t *wpp;
740027
   wchar_t *month_wcs;
740027
   mbstate_t state;
740027
 
740027
@@ -2875,17 +2875,19 @@ getmonth_mb (const char *s, size_t len, char **ea)
740027
   if (len == 0)
740027
     return 0;
740027
 
740027
-  month = (char *) xmalloc (len + 1);
740027
+  if (SIZE_MAX - len < 1)
740027
+    xalloc_die ();
740027
+
740027
+  month = (char *) xnmalloc (len + 1, MB_CUR_MAX);
740027
 
740027
-  tmp = (char *) xmalloc (len + 1);
740027
+  pp = tmp = (char *) xnmalloc (len + 1, MB_CUR_MAX);
740027
   memcpy (tmp, s, len);
740027
   tmp[len] = '\0';
740027
-  pp = (const char **)&tm;;
740027
-  month_wcs = (wchar_t *) xmalloc ((len + 1) * sizeof (wchar_t));
740027
-  memset (&state, '\0', sizeof(mbstate_t));
740027
+  wpp = month_wcs = (wchar_t *) xnmalloc (len + 1, sizeof (wchar_t));
740027
+  memset (&state, '\0', sizeof (mbstate_t));
740027
 
740027
-  wclength = mbsrtowcs (month_wcs, pp, len + 1, &state);
740027
-  if (wclength == (size_t)-1 || *pp != NULL)
740027
+  wclength = mbsrtowcs (month_wcs, &pp, len + 1, &state);
740027
+  if (wclength == (size_t)-1 || pp != NULL)
740027
     error (SORT_FAILURE, 0, _("Invalid multibyte input %s."), quote(s));
740027
 
740027
   for (i = 0; i < wclength; i++)
740027
@@ -2898,10 +2900,8 @@ getmonth_mb (const char *s, size_t len, char **ea)
740027
         }
740027
     }
740027
 
740027
-  wpp = (const wchar_t **)&month_wcs;
740027
-
740027
-  mblength = wcsrtombs (month, wpp, len + 1, &state);
740027
-  assert (mblength != (-1) && *wpp == NULL);
740027
+  mblength = wcsrtombs (month, &wpp, (len + 1) * MB_CUR_MAX, &state);
740027
+  assert (mblength != (-1) && wpp == NULL);
740027
 
740027
   do
740027
     {
740027
@@ -5363,10 +5363,10 @@ main (int argc, char **argv)
740027
 
740027
   if (nfiles == 0)
740027
     {
740027
-      static char *minus = (char *) "-";
740027
       nfiles = 1;
740027
       free (files);
740027
-      files = −
740027
+      files = xmalloc (sizeof *files);
740027
+      *files = (char *) "-";
740027
     }
740027
 
740027
   /* Need to re-check that we meet the minimum requirement for memory
740027
@@ -5424,6 +5424,13 @@ main (int argc, char **argv)
740027
       sort (files, nfiles, outfile, nthreads);
740027
     }
740027
 
740027
+#ifdef lint
740027
+  if (files_from)
740027
+    readtokens0_free (&tok;;
740027
+  else
740027
+    free (files);
740027
+#endif
740027
+
740027
   if (have_read_stdin && fclose (stdin) == EOF)
740027
     die (_("close failed"), "-");
740027
 
740027
-- 
740027
2.14.4
740027