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

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