grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v13 1/3] safemath: Add ALIGN_UP_OVF() that checks for {over, unde


From: Gao Xiang
Subject: [PATCH v13 1/3] safemath: Add ALIGN_UP_OVF() that checks for {over, under}flow
Date: Fri, 17 May 2024 20:17:26 +0800

The following EROFS patch will use this helper to handle overflow
ALIGN_UP() cases.

Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
---
 include/grub/safemath.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/grub/safemath.h b/include/grub/safemath.h
index fbd9b5925..baaea0ef4 100644
--- a/include/grub/safemath.h
+++ b/include/grub/safemath.h
@@ -32,6 +32,22 @@
 
 #define grub_cast(a, res)      grub_add ((a), 0, (res))
 
+#define ALIGN_UP_OVF(v, align, res)                    \
+({                                                     \
+  bool __failed;                                       \
+  typeof(v) a;                                         \
+                                                       \
+  __failed = grub_sub ((typeof(v))(align), 1, &a);     \
+  if (__failed == false)                               \
+    {                                                  \
+    __failed = grub_add (v, a, res);                   \
+    if (__failed == false)                             \
+      {                                                        \
+        *(res) &= ~a;                                  \
+      }                                                        \
+    }                                                  \
+__failed;})
+
 #else
 #error gcc 5.1 or newer or clang 8.0 or newer is required
 #endif
-- 
2.39.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]