]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
safemath: Add ALIGN_UP_OVF() which checks for an overflow
authorGao Xiang <hsiangkao@linux.alibaba.com>
Mon, 20 May 2024 17:20:57 +0000 (01:20 +0800)
committerDaniel Kiper <daniel.kiper@oracle.com>
Thu, 23 May 2024 13:19:06 +0000 (15:19 +0200)
The following EROFS patch will use this helper to handle
ALIGN_UP() overflow.

Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
include/grub/safemath.h

index fbd9b5925769c6492484f8c85de9c4eebfa76e60..e032f63a0ecef6e9a50deb24faab4c6440f47c0a 100644 (file)
 
 #define grub_cast(a, res)      grub_add ((a), 0, (res))
 
+/*
+ * It's caller's responsibility to check "align" does not equal 0 and
+ * is power of 2.
+ */
+#define ALIGN_UP_OVF(v, align, res)                    \
+({                                                     \
+  bool __failed;                                       \
+  typeof(v) __a = ((typeof(v))(align) - 1);            \
+                                                       \
+  __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