nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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