Blob Blame History Raw
From e16ae8b0f8a16f05d5881ad282bd58b31645a34f Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Fri, 15 Jun 2018 12:47:38 +0200
Subject: [PATCH] Resolves: #1259942 - fix memory leak in sort/I18N
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Patches written by Pádraig.

Note that the corresponding i18n/sort-month test was not included
because it breaks unless sort is compiled -Dlint and we do not want
to decrease performance of the resulting RPMs (and valgrind is not
installed in production buildroots anyway).
---
 src/sort.c | 37 ++++++++++++++++++++++---------------
 1 file changed, 22 insertions(+), 15 deletions(-)

diff --git a/src/sort.c b/src/sort.c
index e47b039..c04b513 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -2861,8 +2861,8 @@ getmonth_mb (const char *s, size_t len, char **ea)
   register int lo = 0, hi = MONTHS_PER_YEAR, result;
   char *tmp;
   size_t wclength, mblength;
-  const char **pp;
-  const wchar_t **wpp;
+  const char *pp;
+  const wchar_t *wpp;
   wchar_t *month_wcs;
   mbstate_t state;
 
@@ -2875,17 +2875,19 @@ getmonth_mb (const char *s, size_t len, char **ea)
   if (len == 0)
     return 0;
 
-  month = (char *) xmalloc (len + 1);
+  if (SIZE_MAX - len < 1)
+    xalloc_die ();
+
+  month = (char *) xnmalloc (len + 1, MB_CUR_MAX);
 
-  tmp = (char *) xmalloc (len + 1);
+  pp = tmp = (char *) xnmalloc (len + 1, MB_CUR_MAX);
   memcpy (tmp, s, len);
   tmp[len] = '\0';
-  pp = (const char **)&tmp;
-  month_wcs = (wchar_t *) xmalloc ((len + 1) * sizeof (wchar_t));
-  memset (&state, '\0', sizeof(mbstate_t));
+  wpp = month_wcs = (wchar_t *) xnmalloc (len + 1, sizeof (wchar_t));
+  memset (&state, '\0', sizeof (mbstate_t));
 
-  wclength = mbsrtowcs (month_wcs, pp, len + 1, &state);
-  if (wclength == (size_t)-1 || *pp != NULL)
+  wclength = mbsrtowcs (month_wcs, &pp, len + 1, &state);
+  if (wclength == (size_t)-1 || pp != NULL)
     error (SORT_FAILURE, 0, _("Invalid multibyte input %s."), quote(s));
 
   for (i = 0; i < wclength; i++)
@@ -2898,10 +2900,8 @@ getmonth_mb (const char *s, size_t len, char **ea)
         }
     }
 
-  wpp = (const wchar_t **)&month_wcs;
-
-  mblength = wcsrtombs (month, wpp, len + 1, &state);
-  assert (mblength != (-1) && *wpp == NULL);
+  mblength = wcsrtombs (month, &wpp, (len + 1) * MB_CUR_MAX, &state);
+  assert (mblength != (-1) && wpp == NULL);
 
   do
     {
@@ -5363,10 +5363,10 @@ main (int argc, char **argv)
 
   if (nfiles == 0)
     {
-      static char *minus = (char *) "-";
       nfiles = 1;
       free (files);
-      files = &minus;
+      files = xmalloc (sizeof *files);
+      *files = (char *) "-";
     }
 
   /* Need to re-check that we meet the minimum requirement for memory
@@ -5424,6 +5424,13 @@ main (int argc, char **argv)
       sort (files, nfiles, outfile, nthreads);
     }
 
+#ifdef lint
+  if (files_from)
+    readtokens0_free (&tok);
+  else
+    free (files);
+#endif
+
   if (have_read_stdin && fclose (stdin) == EOF)
     die (_("close failed"), "-");
 
-- 
2.14.4