From e8a587ea7b53ad9a7e66d76e5142afd4d7173587 Mon Sep 17 00:00:00 2001 From: Berkus Decker Date: Fri, 11 Aug 2023 17:22:16 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Don't=20overflow=20calcul?= =?UTF-8?q?ations=20in=20align=5Fup?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machine/src/mm/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/machine/src/mm/mod.rs b/machine/src/mm/mod.rs index 796b1f7..a1e232d 100644 --- a/machine/src/mm/mod.rs +++ b/machine/src/mm/mod.rs @@ -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.