Blame SOURCES/0463-kern-parser-Refactor-grub_parser_split_cmdline-clean.patch

9723a8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
9723a8
From: Chris Coulson <chris.coulson@canonical.com>
9723a8
Date: Wed, 6 Jan 2021 13:54:26 +0000
9723a8
Subject: [PATCH] kern/parser: Refactor grub_parser_split_cmdline() cleanup
9723a8
9723a8
Introduce a common function epilogue used for cleaning up on all
9723a8
return paths, which will simplify additional error handling to be
9723a8
introduced in a subsequent commit.
9723a8
9723a8
Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
9723a8
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
9723a8
---
9723a8
 grub-core/kern/parser.c | 35 ++++++++++++++++++++---------------
9723a8
 1 file changed, 20 insertions(+), 15 deletions(-)
9723a8
9723a8
diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
9723a8
index 572c67089f3..e010eaa1fa1 100644
9723a8
--- a/grub-core/kern/parser.c
9723a8
+++ b/grub-core/kern/parser.c
9723a8
@@ -221,19 +221,13 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
 
9723a8
 	  if (process_char (*rp, buffer, &bp, varname, &vp, state, argc,
9723a8
 			    &newstate) != GRUB_ERR_NONE)
9723a8
-	    {
9723a8
-	      if (rd != cmdline)
9723a8
-		grub_free (rd);
9723a8
-	      return grub_errno;
9723a8
-	    }
9723a8
+	    goto fail;
9723a8
+
9723a8
 	  state = newstate;
9723a8
 	}
9723a8
     }
9723a8
   while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
9723a8
 
9723a8
-  if (rd != cmdline)
9723a8
-    grub_free (rd);
9723a8
-
9723a8
   /* A special case for when the last character was part of a
9723a8
      variable.  */
9723a8
   add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT);
9723a8
@@ -243,20 +237,20 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
 
9723a8
   /* If there are no args, then we're done. */
9723a8
   if (!*argc)
9723a8
-    return 0;
9723a8
+    {
9723a8
+      grub_errno = GRUB_ERR_NONE;
9723a8
+      goto out;
9723a8
+    }
9723a8
 
9723a8
   /* Reserve memory for the return values.  */
9723a8
   args = grub_malloc (bp - buffer);
9723a8
   if (!args)
9723a8
-    return grub_errno;
9723a8
+    goto fail;
9723a8
   grub_memcpy (args, buffer, bp - buffer);
9723a8
 
9723a8
   *argv = grub_calloc (*argc + 1, sizeof (char *));
9723a8
   if (!*argv)
9723a8
-    {
9723a8
-      grub_free (args);
9723a8
-      return grub_errno;
9723a8
-    }
9723a8
+    goto fail;
9723a8
 
9723a8
   /* The arguments are separated with 0's, setup argv so it points to
9723a8
      the right values.  */
9723a8
@@ -269,7 +263,18 @@ grub_parser_split_cmdline (const char *cmdline,
9723a8
       bp++;
9723a8
     }
9723a8
 
9723a8
-  return 0;
9723a8
+  grub_errno = GRUB_ERR_NONE;
9723a8
+
9723a8
+ out:
9723a8
+  if (rd != cmdline)
9723a8
+    grub_free (rd);
9723a8
+
9723a8
+  return grub_errno;
9723a8
+
9723a8
+ fail:
9723a8
+  grub_free (*argv);
9723a8
+  grub_free (args);
9723a8
+  goto out;
9723a8
 }
9723a8
 
9723a8
 /* Helper for grub_parser_execute.  */