1From 8bc817014ce3d7a498db44eae33c8b90e2430926 Mon Sep 17 00:00:00 2001
2From: Chris Coulson <chris.coulson@canonical.com>
3Date: Wed, 6 Jan 2021 13:54:26 +0000
4Subject: [PATCH] kern/parser: Refactor grub_parser_split_cmdline() cleanup
5
6Introduce a common function epilogue used for cleaning up on all
7return paths, which will simplify additional error handling to be
8introduced in a subsequent commit.
9
10Signed-off-by: Chris Coulson <chris.coulson@canonical.com>
11Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
12Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
13---
14 grub-core/kern/parser.c | 35 ++++++++++++++++++++---------------
15 1 file changed, 20 insertions(+), 15 deletions(-)
16
17diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
18index 572c670..e010eaa 100644
19--- a/grub-core/kern/parser.c
20+++ b/grub-core/kern/parser.c
21@@ -221,19 +221,13 @@ grub_parser_split_cmdline (const char *cmdline,
22
23 	  if (process_char (*rp, buffer, &bp, varname, &vp, state, argc,
24 			    &newstate) != GRUB_ERR_NONE)
25-	    {
26-	      if (rd != cmdline)
27-		grub_free (rd);
28-	      return grub_errno;
29-	    }
30+	    goto fail;
31+
32 	  state = newstate;
33 	}
34     }
35   while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
36
37-  if (rd != cmdline)
38-    grub_free (rd);
39-
40   /* A special case for when the last character was part of a
41      variable.  */
42   add_var (varname, &bp, &vp, state, GRUB_PARSER_STATE_TEXT);
43@@ -243,20 +237,20 @@ grub_parser_split_cmdline (const char *cmdline,
44
45   /* If there are no args, then we're done. */
46   if (!*argc)
47-    return 0;
48+    {
49+      grub_errno = GRUB_ERR_NONE;
50+      goto out;
51+    }
52
53   /* Reserve memory for the return values.  */
54   args = grub_malloc (bp - buffer);
55   if (!args)
56-    return grub_errno;
57+    goto fail;
58   grub_memcpy (args, buffer, bp - buffer);
59
60   *argv = grub_calloc (*argc + 1, sizeof (char *));
61   if (!*argv)
62-    {
63-      grub_free (args);
64-      return grub_errno;
65-    }
66+    goto fail;
67
68   /* The arguments are separated with 0's, setup argv so it points to
69      the right values.  */
70@@ -269,7 +263,18 @@ grub_parser_split_cmdline (const char *cmdline,
71       bp++;
72     }
73
74-  return 0;
75+  grub_errno = GRUB_ERR_NONE;
76+
77+ out:
78+  if (rd != cmdline)
79+    grub_free (rd);
80+
81+  return grub_errno;
82+
83+ fail:
84+  grub_free (*argv);
85+  grub_free (args);
86+  goto out;
87 }
88
89 /* Helper for grub_parser_execute.  */
90--
912.14.2
92
93