Blame SOURCES/coreutils-i18n-sort-memleak.patch

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