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 <hsiangkao@linux.alibaba.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Gao Xiang 2024-05-21 01:20:57 +08:00 committed by Daniel Kiper
parent d291449ba3
commit 1ba39de62f

View File

@ -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