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

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