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

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