nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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