Blame SOURCES/0391-script-execute-Avoid-crash-when-using-outside-a-func.patch

b1bcb2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b1bcb2
From: Daniel Axtens <dja@axtens.net>
b1bcb2
Date: Mon, 11 Jan 2021 17:30:42 +1100
b1bcb2
Subject: [PATCH] script/execute: Avoid crash when using "$#" outside a
b1bcb2
 function scope
b1bcb2
b1bcb2
"$#" represents the number of arguments to a function. It is only
b1bcb2
defined in a function scope, where "scope" is non-NULL. Currently,
b1bcb2
if we attempt to evaluate "$#" outside a function scope, "scope" will
b1bcb2
be NULL and we will crash with a NULL pointer dereference.
b1bcb2
b1bcb2
Do not attempt to count arguments for "$#" if "scope" is NULL. This
b1bcb2
will result in "$#" being interpreted as an empty string if evaluated
b1bcb2
outside a function scope.
b1bcb2
b1bcb2
Signed-off-by: Daniel Axtens <dja@axtens.net>
b1bcb2
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
b1bcb2
---
b1bcb2
 grub-core/script/execute.c | 2 +-
b1bcb2
 1 file changed, 1 insertion(+), 1 deletion(-)
b1bcb2
b1bcb2
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
b1bcb2
index 04ffb9b4740..7c43f88ac51 100644
b1bcb2
--- a/grub-core/script/execute.c
b1bcb2
+++ b/grub-core/script/execute.c
b1bcb2
@@ -518,7 +518,7 @@ gettext_putvar (const char *str, grub_size_t len,
b1bcb2
     return 0;
b1bcb2
 
b1bcb2
   /* Enough for any number.  */
b1bcb2
-  if (len == 1 && str[0] == '#')
b1bcb2
+  if (len == 1 && str[0] == '#' && scope != NULL)
b1bcb2
     {
b1bcb2
       grub_snprintf (*ptr, 30, "%u", scope->argv.argc);
b1bcb2
       *ptr += grub_strlen (*ptr);