From: Gao Xiang Date: Mon, 20 May 2024 17:20:57 +0000 (+0800) Subject: safemath: Add ALIGN_UP_OVF() which checks for an overflow X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ba39de62f91302f7eb95e78dc8ce9c086f74df8;p=thirdparty%2Fgrub.git safemath: Add ALIGN_UP_OVF() which checks for an overflow The following EROFS patch will use this helper to handle ALIGN_UP() overflow. Signed-off-by: Gao Xiang Reviewed-by: Daniel Kiper --- diff --git a/include/grub/safemath.h b/include/grub/safemath.h index fbd9b5925..e032f63a0 100644 --- a/include/grub/safemath.h +++ b/include/grub/safemath.h @@ -32,6 +32,21 @@ #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