1 Star 0 Fork 49

Zhao Hang / grub2

forked from src-anolis-os / grub2 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
0305-update-safemath-with-fallback-code-for-gcc-older-tha.patch 5.37 KB
一键复制 编辑 原始数据 按行查看 历史
geliwei 提交于 2022-04-13 15:15 . update to grub2-2.02-120.el8.src.rpm
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Alexander Burmashev <alexander.burmashev@oracle.com>
Date: Wed, 22 Jul 2020 06:04:38 -0700
Subject: [PATCH] update safemath with fallback code for gcc older than 5.1
The code used in the header was taken from linux kernel commit
f0907827a8a9152aedac2833ed1b674a7b2a44f2. Rasmus Villemoes
<linux@rasmusvillemoes.dk>, the original author of the patch, was
contacted directly, confirmed his authorship of the code, and gave his
permission on treating that dual license as MIT and including into GRUB2
sources
Signed-off-by: Alex Burmashev <alexander.burmashev@oracle.com>
---
include/grub/safemath.h | 119 +++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 118 insertions(+), 1 deletion(-)
diff --git a/include/grub/safemath.h b/include/grub/safemath.h
index c17b89bba..1ccac276b 100644
--- a/include/grub/safemath.h
+++ b/include/grub/safemath.h
@@ -31,7 +31,124 @@
#define grub_mul(a, b, res) __builtin_mul_overflow(a, b, res)
#else
-#error gcc 5.1 or newer or clang 3.8 or newer is required
+/*
+ * Copyright 2020 Rasmus Villemoes
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+/*
+ * The code used in this header was taken from linux kernel commit
+ * f0907827a8a9152aedac2833ed1b674a7b2a44f2
+ * Rasmus Villemoes <linux@rasmusvillemoes.dk>, the original author of the
+ * patch, was contacted directly, confirmed his authorship of the code, and
+ * gave his permission on treating that dual license as MIT and including into
+ * GRUB2 sources
+ */
+
+#include <grub/types.h>
+#define is_signed_type(type) (((type)(-1)) < (type)1)
+#define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
+#define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
+#define type_min(T) ((T)((T)-type_max(T)-(T)1))
+
+#define __unsigned_add_overflow(a, b, d) ({ \
+ typeof(+(a)) __a = (a); \
+ typeof(+(b)) __b = (b); \
+ typeof(d) __d = (d); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ *__d = __a + __b; \
+ *__d < __a; \
+})
+#define __unsigned_sub_overflow(a, b, d) ({ \
+ typeof(+(a)) __a = (a); \
+ typeof(+(b)) __b = (b); \
+ typeof(d) __d = (d); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ *__d = __a - __b; \
+ __a < __b; \
+})
+#define __unsigned_mul_overflow(a, b, d) ({ \
+ typeof(+(a)) __a = (a); \
+ typeof(+(b)) __b = (b); \
+ typeof(d) __d = (d); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ *__d = __a * __b; \
+ __builtin_constant_p(__b) ? \
+ __b > 0 && __a > type_max(typeof(__a)) / __b :\
+ __a > 0 && __b > type_max(typeof(__b)) / __a; \
+})
+
+#define __signed_add_overflow(a, b, d) ({ \
+ typeof(+(a)) __a = (a); \
+ typeof(+(b)) __b = (b); \
+ typeof(d) __d = (d); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ *__d = (grub_uint64_t)__a + (grub_uint64_t)__b; \
+ (((~(__a ^ __b)) & (*__d ^ __a)) \
+ & type_min(typeof(__a))) != 0; \
+})
+
+#define __signed_sub_overflow(a, b, d) ({ \
+ typeof(+(a)) __a = (a); \
+ typeof(+(b)) __b = (b); \
+ typeof(d) __d = (d); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ *__d = (grub_uint64_t)__a - (grub_uint64_t)__b; \
+ ((((__a ^ __b)) & (*__d ^ __a)) \
+ & type_min(typeof(__a))) != 0; \
+})
+
+#define __signed_mul_overflow(a, b, d) ({ \
+ typeof(+(a)) __a = (a); \
+ typeof(+(b)) __b = (b); \
+ typeof(d) __d = (d); \
+ typeof(+(a)) __tmax = type_max(typeof(+(a))); \
+ typeof(+(a)) __tmin = type_min(typeof(+(a))); \
+ (void) (&__a == &__b); \
+ (void) (&__a == __d); \
+ *__d = (grub_uint64_t)__a * (grub_uint64_t)__b; \
+ (__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) ||\
+ (__b < (typeof(__b))-1 && \
+ (__a > __tmin/__b || __a < __tmax/__b)) || \
+ (__b == (typeof(__b))-1 && __a == __tmin); \
+})
+
+#define grub_add(a, b, d) \
+ __builtin_choose_expr(is_signed_type(typeof(+(a))), \
+ __signed_add_overflow(a, b, d), \
+ __unsigned_add_overflow(a, b, d))
+
+#define grub_sub(a, b, d) \
+ __builtin_choose_expr(is_signed_type(typeof(+(a))), \
+ __signed_sub_overflow(a, b, d), \
+ __unsigned_sub_overflow(a, b, d))
+
+#define grub_mul(a, b, d) \
+ __builtin_choose_expr(is_signed_type(typeof(+(a))), \
+ __signed_mul_overflow(a, b, d), \
+ __unsigned_mul_overflow(a, b, d))
+
#endif
#endif /* GRUB_SAFEMATH_H */
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhaohang_mskdxl/grub2.git
git@gitee.com:zhaohang_mskdxl/grub2.git
zhaohang_mskdxl
grub2
grub2
a8

搜索帮助

344bd9b3 5694891 D2dac590 5694891