Blame SOURCES/0460-kern-parser-Fix-a-memory-leak.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Chris Coulson <chris.coulson@canonical.com>
80913e
Date: Wed, 18 Nov 2020 00:59:24 +0000
80913e
Subject: [PATCH] kern/parser: Fix a memory leak
80913e
80913e
The getline() function supplied to grub_parser_split_cmdline() returns
80913e
a newly allocated buffer and can be called multiple times, but the
80913e
returned buffer is never freed.
80913e
80913e
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
---
80913e
 grub-core/kern/parser.c | 20 ++++++++++++++++----
80913e
 1 file changed, 16 insertions(+), 4 deletions(-)
80913e
80913e
diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
80913e
index d1cf061ad68..39e4df65b86 100644
80913e
--- a/grub-core/kern/parser.c
80913e
+++ b/grub-core/kern/parser.c
80913e
@@ -140,6 +140,7 @@ grub_parser_split_cmdline (const char *cmdline,
80913e
   char buffer[1024];
80913e
   char *bp = buffer;
80913e
   char *rd = (char *) cmdline;
80913e
+  char *rp = rd;
80913e
   char varname[200];
80913e
   char *vp = varname;
80913e
   char *args;
80913e
@@ -149,10 +150,18 @@ grub_parser_split_cmdline (const char *cmdline,
80913e
   *argv = NULL;
80913e
   do
80913e
     {
80913e
-      if (!rd || !*rd)
80913e
+      if (rp == NULL || *rp == '\0')
80913e
 	{
80913e
+	  if (rd != cmdline)
80913e
+	    {
80913e
+	      grub_free (rd);
80913e
+	      rd = rp = NULL;
80913e
+	    }
80913e
 	  if (getline)
80913e
-	    getline (&rd, 1, getline_data);
80913e
+	    {
80913e
+	      getline (&rd, 1, getline_data);
80913e
+	      rp = rd;
80913e
+	    }
80913e
 	  else
80913e
 	    break;
80913e
 	}
80913e
@@ -160,12 +169,12 @@ grub_parser_split_cmdline (const char *cmdline,
80913e
       if (!rd)
80913e
 	break;
80913e
 
80913e
-      for (; *rd; rd++)
80913e
+      for (; *rp != '\0'; rp++)
80913e
 	{
80913e
 	  grub_parser_state_t newstate;
80913e
 	  char use;
80913e
 
80913e
-	  newstate = grub_parser_cmdline_state (state, *rd, &use;;
80913e
+	  newstate = grub_parser_cmdline_state (state, *rp, &use;;
80913e
 
80913e
 	  /* If a variable was being processed and this character does
80913e
 	     not describe the variable anymore, write the variable to
80913e
@@ -198,6 +207,9 @@ grub_parser_split_cmdline (const char *cmdline,
80913e
     }
80913e
   while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
80913e
 
80913e
+  if (rd != cmdline)
80913e
+    grub_free (rd);
80913e
+
80913e
   /* A special case for when the last character was part of a
80913e
      variable.  */
80913e
   add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT);