fix: 🐛 Don't overflow calculations in align_up

This commit is contained in:
Berkus Decker 2023-08-11 17:22:16 +03:00 committed by Berkus Decker
parent a656a9bdd7
commit e8a587ea7b
1 changed files with 6 additions and 1 deletions

View File

@ -30,7 +30,12 @@ pub const fn align_up(value: usize, alignment: usize) -> usize {
"`alignment` must be a power of two"
);
(value + alignment - 1) & !(alignment - 1)
let align_mask = alignment - 1;
if value & align_mask == 0 {
value // already aligned
} else {
(value | align_mask) + 1
}
}
/// Check if a value is aligned to a given alignment.