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

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