Blame SOURCES/0461-kern-parser-Introduce-process_char-helper.patch

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