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

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