macro: avoid subtraction overflow in ALIGN_POWER2()

This commit is contained in:
Lennart Poettering
2019-12-09 18:34:05 +01:00
parent fb4b0465ab
commit 85c267afa7
+5
View File
@@ -163,6 +163,11 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
/* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */
static inline unsigned long ALIGN_POWER2(unsigned long u) {
/* Avoid subtraction overflow */
if (u == 0)
return 0;
/* clz(0) is undefined */
if (u == 1)
return 1;