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

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