nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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