dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0323-update-safemath-with-fallback-code-for-gcc-older-tha.patch

c4e390
From 6c4c027abfb58272a3604db4094733b4388c1638 Mon Sep 17 00:00:00 2001
c4e390
From: Alexander Burmashev <alexander.burmashev@oracle.com>
c4e390
Date: Wed, 22 Jul 2020 06:04:38 -0700
c4e390
Subject: [PATCH 323/336] update safemath with fallback code for gcc older than
c4e390
 5.1
c4e390
c4e390
The code used in the header was taken from linux kernel commit
c4e390
f0907827a8a9152aedac2833ed1b674a7b2a44f2.  Rasmus Villemoes
c4e390
<linux@rasmusvillemoes.dk>, the original author of the patch, was
c4e390
contacted directly, confirmed his authorship of the code, and gave his
c4e390
permission on treating that dual license as MIT and including into GRUB2
c4e390
sources
c4e390
c4e390
Signed-off-by: Alex Burmashev <alexander.burmashev@oracle.com>
c4e390
---
c4e390
 include/grub/safemath.h | 119 +++++++++++++++++++++++++++++++++++++++-
c4e390
 1 file changed, 118 insertions(+), 1 deletion(-)
c4e390
c4e390
diff --git a/include/grub/safemath.h b/include/grub/safemath.h
c4e390
index c17b89bba17..1ccac276b59 100644
c4e390
--- a/include/grub/safemath.h
c4e390
+++ b/include/grub/safemath.h
c4e390
@@ -31,7 +31,124 @@
c4e390
 #define grub_mul(a, b, res)	__builtin_mul_overflow(a, b, res)
c4e390
 
c4e390
 #else
c4e390
-#error gcc 5.1 or newer or clang 3.8 or newer is required
c4e390
+/*
c4e390
+ * Copyright 2020 Rasmus Villemoes
c4e390
+ *
c4e390
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
c4e390
+ * of this software and associated documentation files (the "Software"), to
c4e390
+ * deal in the Software without restriction, including without limitation the
c4e390
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
c4e390
+ * sell copies of the Software, and to permit persons to whom the Software is
c4e390
+ * furnished to do so, subject to the following conditions:
c4e390
+ *
c4e390
+ * The above copyright notice and this permission notice shall be included in
c4e390
+ * all copies or substantial portions of the Software.
c4e390
+
c4e390
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c4e390
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c4e390
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
c4e390
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c4e390
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
c4e390
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
c4e390
+ * IN THE SOFTWARE.
c4e390
+ */
c4e390
+/*
c4e390
+ * The code used in this header was taken from linux kernel commit
c4e390
+ * f0907827a8a9152aedac2833ed1b674a7b2a44f2
c4e390
+ * Rasmus Villemoes <linux@rasmusvillemoes.dk>, the original author of the
c4e390
+ * patch, was contacted directly, confirmed his authorship of the code, and
c4e390
+ * gave his permission on treating that dual license as MIT and including into
c4e390
+ * GRUB2 sources
c4e390
+ */
c4e390
+
c4e390
+#include <grub/types.h>
c4e390
+#define is_signed_type(type)	(((type)(-1)) < (type)1)
c4e390
+#define __type_half_max(type)	((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
c4e390
+#define type_max(T)		((T)((__type_half_max(T) - 1) + __type_half_max(T)))
c4e390
+#define type_min(T)		((T)((T)-type_max(T)-(T)1))
c4e390
+
c4e390
+#define __unsigned_add_overflow(a, b, d) ({	\
c4e390
+	typeof(+(a)) __a = (a);			\
c4e390
+	typeof(+(b)) __b = (b);			\
c4e390
+	typeof(d) __d = (d);			\
c4e390
+	(void) (&__a == &__b);			\
c4e390
+	(void) (&__a == __d);			\
c4e390
+	*__d = __a + __b;			\
c4e390
+	*__d < __a;				\
c4e390
+})
c4e390
+#define __unsigned_sub_overflow(a, b, d) ({     \
c4e390
+	typeof(+(a)) __a = (a);			\
c4e390
+	typeof(+(b)) __b = (b);			\
c4e390
+	typeof(d) __d = (d);			\
c4e390
+	(void) (&__a == &__b);			\
c4e390
+	(void) (&__a == __d);			\
c4e390
+	*__d = __a - __b;			\
c4e390
+	__a < __b;				\
c4e390
+})
c4e390
+#define __unsigned_mul_overflow(a, b, d) ({		\
c4e390
+	typeof(+(a)) __a = (a);				\
c4e390
+	typeof(+(b)) __b = (b);				\
c4e390
+	typeof(d) __d = (d);				\
c4e390
+	(void) (&__a == &__b);				\
c4e390
+	(void) (&__a == __d);				\
c4e390
+	*__d = __a * __b;				\
c4e390
+	__builtin_constant_p(__b) ?			\
c4e390
+	  __b > 0 && __a > type_max(typeof(__a)) / __b :\
c4e390
+	  __a > 0 && __b > type_max(typeof(__b)) / __a; \
c4e390
+})
c4e390
+
c4e390
+#define __signed_add_overflow(a, b, d) ({		\
c4e390
+	typeof(+(a)) __a = (a);				\
c4e390
+	typeof(+(b)) __b = (b);				\
c4e390
+	typeof(d) __d = (d);				\
c4e390
+	(void) (&__a == &__b);				\
c4e390
+	(void) (&__a == __d);				\
c4e390
+	*__d = (grub_uint64_t)__a + (grub_uint64_t)__b;	\
c4e390
+	(((~(__a ^ __b)) & (*__d ^ __a))		\
c4e390
+		& type_min(typeof(__a))) != 0;		\
c4e390
+})
c4e390
+
c4e390
+#define __signed_sub_overflow(a, b, d) ({		\
c4e390
+	typeof(+(a)) __a = (a);				\
c4e390
+	typeof(+(b)) __b = (b);				\
c4e390
+	typeof(d) __d = (d);				\
c4e390
+	(void) (&__a == &__b);				\
c4e390
+	(void) (&__a == __d);				\
c4e390
+	*__d = (grub_uint64_t)__a - (grub_uint64_t)__b;	\
c4e390
+	((((__a ^ __b)) & (*__d ^ __a))			\
c4e390
+		& type_min(typeof(__a))) != 0;		\
c4e390
+})
c4e390
+
c4e390
+#define __signed_mul_overflow(a, b, d) ({			\
c4e390
+	typeof(+(a)) __a = (a);					\
c4e390
+	typeof(+(b)) __b = (b);					\
c4e390
+	typeof(d) __d = (d);					\
c4e390
+	typeof(+(a)) __tmax = type_max(typeof(+(a)));		\
c4e390
+	typeof(+(a)) __tmin = type_min(typeof(+(a)));		\
c4e390
+	(void) (&__a == &__b);					\
c4e390
+	(void) (&__a == __d);					\
c4e390
+	*__d = (grub_uint64_t)__a * (grub_uint64_t)__b;		\
c4e390
+	(__b > 0   && (__a > __tmax/__b || __a < __tmin/__b)) ||\
c4e390
+	(__b < (typeof(__b))-1  &&				\
c4e390
+	 (__a > __tmin/__b || __a < __tmax/__b)) ||		\
c4e390
+	(__b == (typeof(__b))-1 && __a == __tmin);		\
c4e390
+})
c4e390
+
c4e390
+#define grub_add(a, b, d)					\
c4e390
+	__builtin_choose_expr(is_signed_type(typeof(+(a))),	\
c4e390
+			__signed_add_overflow(a, b, d),		\
c4e390
+			__unsigned_add_overflow(a, b, d))
c4e390
+
c4e390
+#define grub_sub(a, b, d)					\
c4e390
+	__builtin_choose_expr(is_signed_type(typeof(+(a))),	\
c4e390
+			__signed_sub_overflow(a, b, d),		\
c4e390
+			__unsigned_sub_overflow(a, b, d))
c4e390
+
c4e390
+#define grub_mul(a, b, d)					\
c4e390
+	__builtin_choose_expr(is_signed_type(typeof(+(a))),	\
c4e390
+			__signed_mul_overflow(a, b, d),		\
c4e390
+			__unsigned_mul_overflow(a, b, d))
c4e390
+
c4e390
 #endif
c4e390
 
c4e390
 #endif /* GRUB_SAFEMATH_H */
c4e390
-- 
c4e390
2.26.2
c4e390