From e0932b143600beb9eae5495660c6f63e5246aa48 Mon Sep 17 00:00:00 2001 From: Christoph Jabs Date: Fri, 18 Oct 2024 16:39:38 +0300 Subject: [PATCH] feat: check variable addition overflow in debug --- src/types.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/types.rs b/src/types.rs index ff053524..fd4a7736 100644 --- a/src/types.rs +++ b/src/types.rs @@ -212,14 +212,15 @@ impl ops::Add for Var { type Output = Var; fn add(self, rhs: u32) -> Self::Output { - Var { - idx: self.idx + rhs, - } + let idx = self.idx + rhs; + debug_assert!(idx <= Var::MAX_IDX); + Var { idx } } } impl ops::AddAssign for Var { fn add_assign(&mut self, rhs: u32) { + debug_assert!(self.idx + rhs <= Var::MAX_IDX); self.idx += rhs; } }