Blame SOURCES/0541-misc-Make-grub_min-and-grub_max-more-resilient.patch

b9d01e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b9d01e
From: Peter Jones <pjones@redhat.com>
b9d01e
Date: Mon, 21 Mar 2022 16:06:10 -0400
b9d01e
Subject: [PATCH] misc: Make grub_min() and grub_max() more resilient.
b9d01e
b9d01e
grub_min(a,b) and grub_max(a,b) use a relatively naive implementation
b9d01e
which leads to several problems:
b9d01e
- they evaluate their parameters more than once
b9d01e
- the naive way to address this, to declare temporary variables in a
b9d01e
  statement-expression, isn't resilient against nested uses, because
b9d01e
  MIN(a,MIN(b,c)) results in the temporary variables being declared in
b9d01e
  two nested scopes, which may result in a build warning depending on
b9d01e
  your build options.
b9d01e
b9d01e
This patch changes our implementation to use a statement-expression
b9d01e
inside a helper macro, and creates the symbols for the temporary
b9d01e
variables with __COUNTER__ (A GNU C cpp extension) and token pasting to
b9d01e
create uniquely named internal variables.
b9d01e
b9d01e
Signed-off-by: Peter Jones <pjones@redhat.com>
b9d01e
(cherry picked from commit 2d6800450fa731d7b3ef9893986806e88e819eb6)
b9d01e
(cherry picked from commit adaf6a5ae66fb8a23274e3030e9df2714d0fc396)
b9d01e
---
b9d01e
 grub-core/loader/multiboot_elfxx.c |  4 +---
b9d01e
 include/grub/misc.h                | 25 +++++++++++++++++++++++--
b9d01e
 2 files changed, 24 insertions(+), 5 deletions(-)
b9d01e
b9d01e
diff --git a/grub-core/loader/multiboot_elfxx.c b/grub-core/loader/multiboot_elfxx.c
b9d01e
index f2318e0d16..87f6e31aa6 100644
b9d01e
--- a/grub-core/loader/multiboot_elfxx.c
b9d01e
+++ b/grub-core/loader/multiboot_elfxx.c
b9d01e
@@ -35,9 +35,7 @@
b9d01e
 #endif
b9d01e
 
b9d01e
 #include <grub/i386/relocator.h>
b9d01e
-
b9d01e
-#define CONCAT(a,b)	CONCAT_(a, b)
b9d01e
-#define CONCAT_(a,b)	a ## b
b9d01e
+#include <grub/misc.h>
b9d01e
 
b9d01e
 #pragma GCC diagnostic ignored "-Wcast-align"
b9d01e
 
b9d01e
diff --git a/include/grub/misc.h b/include/grub/misc.h
b9d01e
index 6be6a88f65..7ef1a1a87e 100644
b9d01e
--- a/include/grub/misc.h
b9d01e
+++ b/include/grub/misc.h
b9d01e
@@ -35,6 +35,14 @@
b9d01e
 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
b9d01e
 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
b9d01e
 
b9d01e
+#ifndef CONCAT_
b9d01e
+#define CONCAT_(a, b) a ## b
b9d01e
+#endif
b9d01e
+
b9d01e
+#ifndef CONCAT
b9d01e
+#define CONCAT(a, b) CONCAT_(a, b)
b9d01e
+#endif
b9d01e
+
b9d01e
 #define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, __VA_ARGS__)
b9d01e
 
b9d01e
 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
b9d01e
@@ -524,7 +532,20 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
b9d01e
 #define grub_boot_time(...)
b9d01e
 #endif
b9d01e
 
b9d01e
-#define grub_max(a, b) (((a) > (b)) ? (a) : (b))
b9d01e
-#define grub_min(a, b) (((a) < (b)) ? (a) : (b))
b9d01e
+#define _grub_min(a, b, _a, _b)						      \
b9d01e
+  ({ typeof (a) _a = (a);						      \
b9d01e
+     typeof (b) _b = (b);						      \
b9d01e
+     _a < _b ? _a : _b; })
b9d01e
+#define grub_min(a, b) _grub_min(a, b,					      \
b9d01e
+				 CONCAT(_a_,__COUNTER__),		      \
b9d01e
+				 CONCAT(_b_,__COUNTER__))
b9d01e
+
b9d01e
+#define _grub_max(a, b, _a, _b)						      \
b9d01e
+  ({ typeof (a) _a = (a);						      \
b9d01e
+     typeof (b) _b = (b);						      \
b9d01e
+     _a > _b ? _a : _b; })
b9d01e
+#define grub_max(a, b) _grub_max(a, b,					      \
b9d01e
+				 CONCAT(_a_,__COUNTER__),		      \
b9d01e
+				 CONCAT(_b_,__COUNTER__))
b9d01e
 
b9d01e
 #endif /* ! GRUB_MISC_HEADER */