From a4430355a3237fc756480349e9167667b0e89476 Mon Sep 17 00:00:00 2001
From: Glenn Washburn <development@efficientek.com>
Date: Tue, 15 Feb 2022 12:36:43 -0600
Subject: mm: Temporarily disable grub_mm_debug while calling grub_vprintf() in
 grub_printf()

To prevent infinite recursion when grub_mm_debug is on, disable it when
calling grub_vprintf(). One such call loop is:
  grub_vprintf() -> parse_printf_args() -> parse_printf_arg_fmt() ->
    grub_debug_calloc() -> grub_printf() -> grub_vprintf().

Signed-off-by: Glenn Washburn <development@efficientek.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

Conflict:NA
Reference:https://git.savannah.gnu.org/cgit/grub.git/commit?id=a4430355a3237fc756480349e9167667b0e89476

---
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
index de40f56..18bde5d 100644
--- a/grub-core/kern/misc.c
+++ b/grub-core/kern/misc.c
@@ -113,10 +113,30 @@ grub_printf (const char *fmt, ...)
   va_list ap;
   int ret;
 
+#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
+  /*
+   * To prevent infinite recursion when grub_mm_debug is on, disable it
+   * when calling grub_vprintf(). One such call loop is:
+   *   grub_vprintf() -> parse_printf_args() -> parse_printf_arg_fmt() ->
+   *     grub_debug_calloc() -> grub_printf() -> grub_vprintf().
+   */
+  int grub_mm_debug_save = 0;
+
+  if (grub_mm_debug)
+    {
+      grub_mm_debug_save = grub_mm_debug;
+      grub_mm_debug = 0;
+    }
+#endif
+
   va_start (ap, fmt);
   ret = grub_vprintf (fmt, ap);
   va_end (ap);
 
+#if defined(MM_DEBUG) && !defined(GRUB_UTIL) && !defined (GRUB_MACHINE_EMU)
+  grub_mm_debug = grub_mm_debug_save;
+#endif
+
   return ret;
 }
 
-- 
cgit v1.1

