Skip to content

Commit 9e377c4

Browse files
committed
explicitly check for overflow
1 parent e0c5679 commit 9e377c4

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/addr.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,15 +647,20 @@ pub const fn align_down(addr: u64, align: u64) -> u64 {
647647
///
648648
/// Returns the smallest `x` with alignment `align` so that `x >= addr`.
649649
///
650-
/// Panics if the alignment is not a power of two.
650+
/// Panics if the alignment is not a power of two or if an overflow occurs.
651651
#[inline]
652652
pub const fn align_up(addr: u64, align: u64) -> u64 {
653653
assert!(align.is_power_of_two(), "`align` must be a power of two");
654654
let align_mask = align - 1;
655655
if addr & align_mask == 0 {
656656
addr // already aligned
657657
} else {
658-
(addr | align_mask) + 1
658+
// FIXME: Replace with .expect, once `Option::expect` is const.
659+
if let Some(aligned) = (addr | align_mask).checked_add(1) {
660+
aligned
661+
} else {
662+
panic!("attempt to add with overflow")
663+
}
659664
}
660665
}
661666

0 commit comments

Comments
 (0)