From 5ec0762e1855cf144ba52516717c2c364b05ab70 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Fri, 28 Jan 2022 17:22:17 +0100 Subject: [PATCH] Avoid integer overflows by using math ops with wrapping semantics --- packages/storage-plus/benches/main.rs | 32 ++++++++++++++++++--------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/packages/storage-plus/benches/main.rs b/packages/storage-plus/benches/main.rs index cd02624c5..1b5e557b1 100644 --- a/packages/storage-plus/benches/main.rs +++ b/packages/storage-plus/benches/main.rs @@ -27,12 +27,15 @@ fn bench_signed_int_key(c: &mut Criterion) { assert_eq!(to_cw_bytes(&0), i32::to_cw_bytes(&0)); assert_eq!(to_cw_bytes(&k_check), i32::to_cw_bytes(&k_check)); - assert_eq!(to_cw_bytes(&-k_check), i32::to_cw_bytes(&-k_check)); + assert_eq!( + to_cw_bytes(&k_check.wrapping_neg()), + i32::to_cw_bytes(&k_check.wrapping_neg()) + ); b.iter(|| { let k = k(); black_box(to_cw_bytes(&k)); - black_box(to_cw_bytes(&-k)); + black_box(to_cw_bytes(&k.wrapping_neg())); }); }); @@ -44,12 +47,15 @@ fn bench_signed_int_key(c: &mut Criterion) { assert_eq!(to_cw_bytes(&0), i32::to_cw_bytes(&0)); assert_eq!(to_cw_bytes(&k_check), i32::to_cw_bytes(&k_check)); - assert_eq!(to_cw_bytes(&-k_check), i32::to_cw_bytes(&-k_check)); + assert_eq!( + to_cw_bytes(&k_check.wrapping_neg()), + i32::to_cw_bytes(&k_check.wrapping_neg()) + ); b.iter(|| { let k = k(); black_box(to_cw_bytes(&k)); - black_box(to_cw_bytes(&-k)); + black_box(to_cw_bytes(&k.wrapping_neg())); }); }); @@ -63,12 +69,15 @@ fn bench_signed_int_key(c: &mut Criterion) { assert_eq!(to_cw_bytes(&0), i32::to_cw_bytes(&0)); assert_eq!(to_cw_bytes(&k_check), i32::to_cw_bytes(&k_check)); - assert_eq!(to_cw_bytes(&-k_check), i32::to_cw_bytes(&-k_check)); + assert_eq!( + to_cw_bytes(&k_check.wrapping_neg()), + i32::to_cw_bytes(&k_check.wrapping_neg()) + ); b.iter(|| { let k = k(); black_box(to_cw_bytes(&k)); - black_box(to_cw_bytes(&-k)); + black_box(to_cw_bytes(&k.wrapping_neg())); }); }); @@ -76,20 +85,23 @@ fn bench_signed_int_key(c: &mut Criterion) { #[inline] fn to_cw_bytes(value: &i32) -> Buf { if value >= &0i32 { - (*value as u32 - i32::MIN as u32).to_be_bytes() + ((*value as u32).wrapping_sub(i32::MIN as u32)).to_be_bytes() } else { - (*value as u32 + i32::MIN as u32).to_be_bytes() + ((*value as u32).wrapping_add(i32::MIN as u32)).to_be_bytes() } } assert_eq!(to_cw_bytes(&0), i32::to_cw_bytes(&0)); assert_eq!(to_cw_bytes(&k_check), i32::to_cw_bytes(&k_check)); - assert_eq!(to_cw_bytes(&-k_check), i32::to_cw_bytes(&-k_check)); + assert_eq!( + to_cw_bytes(&k_check.wrapping_neg()), + i32::to_cw_bytes(&k_check.wrapping_neg()) + ); b.iter(|| { let k = k(); black_box(to_cw_bytes(&k)); - black_box(to_cw_bytes(&-k)); + black_box(to_cw_bytes(&k.wrapping_neg())); }); });