dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone
468bd4
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
468bd4
From: Chris Coulson <chris.coulson@canonical.com>
468bd4
Date: Tue, 5 Jan 2021 22:17:28 +0000
468bd4
Subject: [PATCH] kern/parser: Introduce process_char() helper
468bd4
468bd4
grub_parser_split_cmdline() iterates over each command line character.
468bd4
In order to add error checking and to simplify the subsequent error
468bd4
handling, split the character processing in to a separate function.
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 | 74 ++++++++++++++++++++++++++++++-------------------
468bd4
 1 file changed, 46 insertions(+), 28 deletions(-)
468bd4
468bd4
diff --git a/grub-core/kern/parser.c b/grub-core/kern/parser.c
468bd4
index 39e4df65b86..0d3582bd874 100644
468bd4
--- a/grub-core/kern/parser.c
468bd4
+++ b/grub-core/kern/parser.c
468bd4
@@ -1,7 +1,7 @@
468bd4
 /* parser.c - the part of the parser that can return partial tokens */
468bd4
 /*
468bd4
  *  GRUB  --  GRand Unified Bootloader
468bd4
- *  Copyright (C) 2005,2007,2009  Free Software Foundation, Inc.
468bd4
+ *  Copyright (C) 2005,2007,2009,2021  Free Software Foundation, Inc.
468bd4
  *
468bd4
  *  GRUB is free software: you can redistribute it and/or modify
468bd4
  *  it under the terms of the GNU General Public License as published by
468bd4
@@ -129,6 +129,46 @@ add_var (char *varname, char **bp, char **vp,
468bd4
     *((*bp)++) = *val;
468bd4
 }
468bd4
 
468bd4
+static grub_err_t
468bd4
+process_char (char c, char *buffer, char **bp, char *varname, char **vp,
468bd4
+	      grub_parser_state_t state, int *argc,
468bd4
+	      grub_parser_state_t *newstate)
468bd4
+{
468bd4
+  char use;
468bd4
+
468bd4
+  *newstate = grub_parser_cmdline_state (state, c, &use;;
468bd4
+
468bd4
+  /*
468bd4
+   * If a variable was being processed and this character does
468bd4
+   * not describe the variable anymore, write the variable to
468bd4
+   * the buffer.
468bd4
+   */
468bd4
+  add_var (varname, bp, vp, state, *newstate);
468bd4
+
468bd4
+  if (check_varstate (*newstate))
468bd4
+    {
468bd4
+      if (use)
468bd4
+	*((*vp)++) = use;
468bd4
+    }
468bd4
+  else if (*newstate == GRUB_PARSER_STATE_TEXT &&
468bd4
+	   state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
468bd4
+    {
468bd4
+      /*
468bd4
+       * Don't add more than one argument if multiple
468bd4
+       * spaces are used.
468bd4
+       */
468bd4
+      if (*bp != buffer && *((*bp) - 1) != '\0')
468bd4
+	{
468bd4
+	  *((*bp)++) = '\0';
468bd4
+	  (*argc)++;
468bd4
+	}
468bd4
+    }
468bd4
+  else if (use)
468bd4
+    *((*bp)++) = use;
468bd4
+
468bd4
+  return GRUB_ERR_NONE;
468bd4
+}
468bd4
+
468bd4
 grub_err_t
468bd4
 grub_parser_split_cmdline (const char *cmdline,
468bd4
 			   grub_reader_getline_t getline, void *getline_data,
468bd4
@@ -172,35 +212,13 @@ grub_parser_split_cmdline (const char *cmdline,
468bd4
       for (; *rp != '\0'; rp++)
468bd4
 	{
468bd4
 	  grub_parser_state_t newstate;
468bd4
-	  char use;
468bd4
 
468bd4
-	  newstate = grub_parser_cmdline_state (state, *rp, &use;;
468bd4
-
468bd4
-	  /* If a variable was being processed and this character does
468bd4
-	     not describe the variable anymore, write the variable to
468bd4
-	     the buffer.  */
468bd4
-	  add_var (varname, &bp, &vp, state, newstate);
468bd4
-
468bd4
-	  if (check_varstate (newstate))
468bd4
-	    {
468bd4
-	      if (use)
468bd4
-		*(vp++) = use;
468bd4
-	    }
468bd4
-	  else
468bd4
+	  if (process_char (*rp, buffer, &bp, varname, &vp, state, argc,
468bd4
+			    &newstate) != GRUB_ERR_NONE)
468bd4
 	    {
468bd4
-	      if (newstate == GRUB_PARSER_STATE_TEXT
468bd4
-		  && state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
468bd4
-		{
468bd4
-		  /* Don't add more than one argument if multiple
468bd4
-		     spaces are used.  */
468bd4
-		  if (bp != buffer && *(bp - 1))
468bd4
-		    {
468bd4
-		      *(bp++) = '\0';
468bd4
-		      (*argc)++;
468bd4
-		    }
468bd4
-		}
468bd4
-	      else if (use)
468bd4
-		*(bp++) = use;
468bd4
+	      if (rd != cmdline)
468bd4
+		grub_free (rd);
468bd4
+	      return grub_errno;
468bd4
 	    }
468bd4
 	  state = newstate;
468bd4
 	}