c2f7a5e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
c2f7a5e
From: Peter Jones <pjones@redhat.com>
c2f7a5e
Date: Mon, 27 Aug 2018 13:14:06 -0400
c2f7a5e
Subject: [PATCH] Make grub_error() more verbose
c2f7a5e
c2f7a5e
Signed-off-by: Peter Jones <pjones@redhat.com>
c2f7a5e
---
c2f7a5e
 grub-core/kern/efi/mm.c | 17 ++++++++++++++---
c2f7a5e
 grub-core/kern/err.c    | 13 +++++++++++--
c2f7a5e
 include/grub/err.h      |  5 ++++-
c2f7a5e
 3 files changed, 29 insertions(+), 6 deletions(-)
c2f7a5e
c2f7a5e
diff --git a/grub-core/kern/efi/mm.c b/grub-core/kern/efi/mm.c
c2f7a5e
index 5cdf6c943f2..7692e63ba24 100644
c2f7a5e
--- a/grub-core/kern/efi/mm.c
c2f7a5e
+++ b/grub-core/kern/efi/mm.c
c2f7a5e
@@ -157,12 +157,20 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address,
c2f7a5e
 
c2f7a5e
   /* Limit the memory access to less than 4GB for 32-bit platforms.  */
c2f7a5e
   if (address > GRUB_EFI_MAX_USABLE_ADDRESS)
c2f7a5e
-    return 0;
c2f7a5e
+    {
c2f7a5e
+      grub_error (GRUB_ERR_BAD_ARGUMENT,
c2f7a5e
+		  N_("invalid memory address (0x%llx > 0x%llx)"),
c2f7a5e
+		  address, GRUB_EFI_MAX_USABLE_ADDRESS);
c2f7a5e
+      return NULL;
c2f7a5e
+    }
c2f7a5e
 
c2f7a5e
   b = grub_efi_system_table->boot_services;
c2f7a5e
   status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &address);
c2f7a5e
   if (status != GRUB_EFI_SUCCESS)
c2f7a5e
-    return 0;
c2f7a5e
+    {
c2f7a5e
+      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
c2f7a5e
+      return NULL;
c2f7a5e
+    }
c2f7a5e
 
c2f7a5e
   if (address == 0)
c2f7a5e
     {
c2f7a5e
@@ -172,7 +180,10 @@ grub_efi_allocate_pages_real (grub_efi_physical_address_t address,
c2f7a5e
       status = efi_call_4 (b->allocate_pages, alloctype, memtype, pages, &address);
c2f7a5e
       grub_efi_free_pages (0, pages);
c2f7a5e
       if (status != GRUB_EFI_SUCCESS)
c2f7a5e
-	return 0;
c2f7a5e
+	{
c2f7a5e
+	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
c2f7a5e
+	  return NULL;
c2f7a5e
+	}
c2f7a5e
     }
c2f7a5e
 
c2f7a5e
   grub_efi_store_alloc (address, pages);
c2f7a5e
diff --git a/grub-core/kern/err.c b/grub-core/kern/err.c
c2f7a5e
index 53c734de70e..aebfe0cf839 100644
c2f7a5e
--- a/grub-core/kern/err.c
c2f7a5e
+++ b/grub-core/kern/err.c
c2f7a5e
@@ -33,15 +33,24 @@ static struct grub_error_saved grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
c2f7a5e
 static int grub_error_stack_pos;
c2f7a5e
 static int grub_error_stack_assert;
c2f7a5e
 
c2f7a5e
+#ifdef grub_error
c2f7a5e
+#undef grub_error
c2f7a5e
+#endif
c2f7a5e
+
c2f7a5e
 grub_err_t
c2f7a5e
-grub_error (grub_err_t n, const char *fmt, ...)
c2f7a5e
+grub_error (grub_err_t n, const char *file, const int line, const char *fmt, ...)
c2f7a5e
 {
c2f7a5e
   va_list ap;
c2f7a5e
+  int m;
c2f7a5e
 
c2f7a5e
   grub_errno = n;
c2f7a5e
 
c2f7a5e
+  m = grub_snprintf (grub_errmsg, sizeof (grub_errmsg), "%s:%d:", file, line);
c2f7a5e
+  if (m < 0)
c2f7a5e
+    m = 0;
c2f7a5e
+
c2f7a5e
   va_start (ap, fmt);
c2f7a5e
-  grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
c2f7a5e
+  grub_vsnprintf (grub_errmsg + m, sizeof (grub_errmsg) - m, _(fmt), ap);
c2f7a5e
   va_end (ap);
c2f7a5e
 
c2f7a5e
   return n;
c2f7a5e
diff --git a/include/grub/err.h b/include/grub/err.h
c2f7a5e
index 1590c688e1d..9b830757d35 100644
c2f7a5e
--- a/include/grub/err.h
c2f7a5e
+++ b/include/grub/err.h
c2f7a5e
@@ -84,7 +84,10 @@ struct grub_error_saved
c2f7a5e
 extern grub_err_t EXPORT_VAR(grub_errno);
c2f7a5e
 extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];
c2f7a5e
 
c2f7a5e
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...);
c2f7a5e
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file, const int line, const char *fmt, ...);
c2f7a5e
+
c2f7a5e
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
c2f7a5e
+
c2f7a5e
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__ ((noreturn));
c2f7a5e
 void EXPORT_FUNC(grub_error_push) (void);
c2f7a5e
 int EXPORT_FUNC(grub_error_pop) (void);