Skip to content

Commit 45f94ad

Browse files
committed
use overflowing add in bvec checksum
prevents debug assersions on overflow
1 parent a94ebeb commit 45f94ad

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lang/p4rs/src/checksum.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ fn bvec_csum(bv: &BitVec<u8, Msb0>) -> BitVec<u8, Msb0> {
8686
let buf = x.to_be_bytes();
8787
let mut c: u16 = 0;
8888
for i in (0..16).step_by(2) {
89-
c += u16::from_be_bytes([buf[i], buf[i + 1]])
89+
let (mut result, overflow) =
90+
c.overflowing_add(u16::from_be_bytes([buf[i], buf[i + 1]]));
91+
if overflow {
92+
result += 1;
93+
}
94+
c = result;
9095
}
9196
let c = !c;
9297
let mut result = bitvec![u8, Msb0; 0u8, 16];

0 commit comments

Comments
 (0)