Blame SOURCES/0125-Fix-menu-entry-selection-based-on-ID-and-title.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Peter Jones <pjones@redhat.com>
8e15ce
Date: Fri, 19 Oct 2018 10:57:52 -0400
8e15ce
Subject: [PATCH] Fix menu entry selection based on ID and title
8e15ce
8e15ce
Currently if grub_strtoul(saved_entry_value, NULL, 0) does not return an
8e15ce
error, we assume the value it has produced is a correct index into our
8e15ce
menu entry list, and do not try to interpret the value as the "id" or
8e15ce
"title" .  In cases where "id" or "title" start with a numeral, this
8e15ce
makes them impossible to use as selection criteria.
8e15ce
8e15ce
This patch splits the search into three phases - matching id, matching
8e15ce
title, and only once those have been exhausted, trying to interpret the
8e15ce
ID as a numeral.  In that case, we also require that the entire string
8e15ce
is numeric, not merely a string with leading numeric characters.
8e15ce
8e15ce
Resolves: rhbz#1640979
8e15ce
8e15ce
Signed-off-by: Peter Jones <pjones@redhat.com>
8e15ce
[javierm: fix menu entry selection based on title]
8e15ce
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
8e15ce
---
8e15ce
 grub-core/normal/menu.c | 141 ++++++++++++++++++++++++------------------------
8e15ce
 1 file changed, 71 insertions(+), 70 deletions(-)
8e15ce
8e15ce
diff --git a/grub-core/normal/menu.c b/grub-core/normal/menu.c
8e15ce
index 37d753d8081..ea714d27176 100644
8e15ce
--- a/grub-core/normal/menu.c
8e15ce
+++ b/grub-core/normal/menu.c
8e15ce
@@ -164,12 +164,12 @@ grub_menu_set_timeout (int timeout)
8e15ce
 }
8e15ce
 
8e15ce
 static int
8e15ce
-menuentry_eq (const char *id, const char *spec)
8e15ce
+menuentry_eq (const char *id, const char *spec, int limit)
8e15ce
 {
8e15ce
   const char *ptr1, *ptr2;
8e15ce
   ptr1 = id;
8e15ce
   ptr2 = spec;
8e15ce
-  while (1)
8e15ce
+  while (limit == -1 || ptr1 - id <= limit)
8e15ce
     {
8e15ce
       if (*ptr2 == '>' && ptr2[1] != '>' && *ptr1 == 0)
8e15ce
 	return ptr2 - spec;
8e15ce
@@ -178,7 +178,11 @@ menuentry_eq (const char *id, const char *spec)
8e15ce
       if (*ptr2 == '>')
8e15ce
 	ptr2++;
8e15ce
       if (*ptr1 != *ptr2)
8e15ce
-	return 0;
8e15ce
+	{
8e15ce
+	  if (limit > -1 && ptr1 - id == limit && !*ptr1 && grub_isspace(*ptr2))
8e15ce
+	    return ptr1 -id -1;
8e15ce
+	  return 0;
8e15ce
+	}
8e15ce
       if (*ptr1 == 0)
8e15ce
 	return ptr1 - id;
8e15ce
       ptr1++;
8e15ce
@@ -187,6 +191,58 @@ menuentry_eq (const char *id, const char *spec)
8e15ce
   return 0;
8e15ce
 }
8e15ce
 
8e15ce
+static int
8e15ce
+get_entry_number_helper(grub_menu_t menu,
8e15ce
+			const char * const val, const char ** const tail)
8e15ce
+{
8e15ce
+  /* See if the variable matches the title of a menu entry.  */
8e15ce
+  int entry = -1;
8e15ce
+  grub_menu_entry_t e;
8e15ce
+  int i;
8e15ce
+
8e15ce
+  for (i = 0, e = menu->entry_list; e; i++)
8e15ce
+    {
8e15ce
+      int l = 0;
8e15ce
+      while (val[l] && !grub_isspace(val[l]))
8e15ce
+	l++;
8e15ce
+
8e15ce
+      if (menuentry_eq (e->id, val, l))
8e15ce
+	{
8e15ce
+	  if (tail)
8e15ce
+	    *tail = val + l;
8e15ce
+	  return i;
8e15ce
+	}
8e15ce
+      e = e->next;
8e15ce
+    }
8e15ce
+
8e15ce
+  for (i = 0, e = menu->entry_list; e; i++)
8e15ce
+    {
8e15ce
+
8e15ce
+      if (menuentry_eq (e->title, val, -1))
8e15ce
+	{
8e15ce
+	  if (tail)
8e15ce
+	    *tail = NULL;
8e15ce
+	  return i;
8e15ce
+	}
8e15ce
+      e = e->next;
8e15ce
+    }
8e15ce
+
8e15ce
+  if (tail)
8e15ce
+    *tail = NULL;
8e15ce
+
8e15ce
+  entry = (int) grub_strtoul (val, tail, 0);
8e15ce
+  if (grub_errno == GRUB_ERR_BAD_NUMBER ||
8e15ce
+      (*tail && **tail && !grub_isspace(**tail)))
8e15ce
+    {
8e15ce
+      entry = -1;
8e15ce
+      if (tail)
8e15ce
+	*tail = NULL;
8e15ce
+      grub_errno = GRUB_ERR_NONE;
8e15ce
+    }
8e15ce
+
8e15ce
+  return entry;
8e15ce
+}
8e15ce
+
8e15ce
 /* Get the first entry number from the value of the environment variable NAME,
8e15ce
    which is a space-separated list of non-negative integers.  The entry number
8e15ce
    which is returned is stripped from the value of NAME.  If no entry number
8e15ce
@@ -196,7 +252,6 @@ get_and_remove_first_entry_number (grub_menu_t menu, const char *name)
8e15ce
 {
8e15ce
   const char *val, *tail;
8e15ce
   int entry;
8e15ce
-  int sz = 0;
8e15ce
 
8e15ce
   val = grub_env_get (name);
8e15ce
   if (! val)
8e15ce
@@ -204,50 +259,24 @@ get_and_remove_first_entry_number (grub_menu_t menu, const char *name)
8e15ce
 
8e15ce
   grub_error_push ();
8e15ce
 
8e15ce
-  entry = (int) grub_strtoul (val, &tail, 0);
8e15ce
+  entry = get_entry_number_helper(menu, val, &tail);
8e15ce
+  if (!(*tail == 0 || grub_isspace(*tail)))
8e15ce
+    entry = -1;
8e15ce
 
8e15ce
-  if (grub_errno == GRUB_ERR_BAD_NUMBER)
8e15ce
+  if (entry >= 0)
8e15ce
     {
8e15ce
-      /* See if the variable matches the title of a menu entry.  */
8e15ce
-      grub_menu_entry_t e = menu->entry_list;
8e15ce
-      int i;
8e15ce
-
8e15ce
-      for (i = 0; e; i++)
8e15ce
-	{
8e15ce
-	  sz = menuentry_eq (e->title, val);
8e15ce
-	  if (sz < 1)
8e15ce
-	    sz = menuentry_eq (e->id, val);
8e15ce
-
8e15ce
-	  if (sz >= 1)
8e15ce
-	    {
8e15ce
-	      entry = i;
8e15ce
-	      break;
8e15ce
-	    }
8e15ce
-	  e = e->next;
8e15ce
-	}
8e15ce
-
8e15ce
-      if (sz > 0)
8e15ce
-	grub_errno = GRUB_ERR_NONE;
8e15ce
-
8e15ce
-      if (! e)
8e15ce
-	entry = -1;
8e15ce
-    }
8e15ce
-
8e15ce
-  if (grub_errno == GRUB_ERR_NONE)
8e15ce
-    {
8e15ce
-      if (sz > 0)
8e15ce
-	tail += sz;
8e15ce
-
8e15ce
       /* Skip whitespace to find the next entry.  */
8e15ce
       while (*tail && grub_isspace (*tail))
8e15ce
 	tail++;
8e15ce
-      grub_env_set (name, tail);
8e15ce
+      if (*tail)
8e15ce
+	grub_env_set (name, tail);
8e15ce
+      else
8e15ce
+	grub_env_unset (name);
8e15ce
     }
8e15ce
   else
8e15ce
     {
8e15ce
       grub_env_unset (name);
8e15ce
       grub_errno = GRUB_ERR_NONE;
8e15ce
-      entry = -1;
8e15ce
     }
8e15ce
 
8e15ce
   grub_error_pop ();
8e15ce
@@ -524,6 +553,7 @@ static int
8e15ce
 get_entry_number (grub_menu_t menu, const char *name)
8e15ce
 {
8e15ce
   const char *val;
8e15ce
+  const char *tail;
8e15ce
   int entry;
8e15ce
 
8e15ce
   val = grub_env_get (name);
8e15ce
@@ -531,38 +561,9 @@ get_entry_number (grub_menu_t menu, const char *name)
8e15ce
     return -1;
8e15ce
 
8e15ce
   grub_error_push ();
8e15ce
-
8e15ce
-  entry = (int) grub_strtoul (val, 0, 0);
8e15ce
-
8e15ce
-  if (grub_errno == GRUB_ERR_BAD_NUMBER)
8e15ce
-    {
8e15ce
-      /* See if the variable matches the title of a menu entry.  */
8e15ce
-      grub_menu_entry_t e = menu->entry_list;
8e15ce
-      int i;
8e15ce
-
8e15ce
-      grub_errno = GRUB_ERR_NONE;
8e15ce
-
8e15ce
-      for (i = 0; e; i++)
8e15ce
-	{
8e15ce
-	  if (menuentry_eq (e->title, val)
8e15ce
-	      || menuentry_eq (e->id, val))
8e15ce
-	    {
8e15ce
-	      entry = i;
8e15ce
-	      break;
8e15ce
-	    }
8e15ce
-	  e = e->next;
8e15ce
-	}
8e15ce
-
8e15ce
-      if (! e)
8e15ce
-	entry = -1;
8e15ce
-    }
8e15ce
-
8e15ce
-  if (grub_errno != GRUB_ERR_NONE)
8e15ce
-    {
8e15ce
-      grub_errno = GRUB_ERR_NONE;
8e15ce
-      entry = -1;
8e15ce
-    }
8e15ce
-
8e15ce
+  entry = get_entry_number_helper(menu, val, &tail);
8e15ce
+  if (tail && *tail != '\0')
8e15ce
+    entry = -1;
8e15ce
   grub_error_pop ();
8e15ce
 
8e15ce
   return entry;