nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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