cbor4ii
cbor4ii copied to clipboard
Ignore overflow for negative number
The v.checked_sub(1) call in the code below cannot overflow, so it can be safely replaced with a simple subtraction:
let types::Negative(v) = <types::Negative<u64>>::decode(reader)?;
let v = i128::from(v);
let v = -v;
let v = v.checked_sub(1)
.ok_or_else(|| dec::Error::arithmetic_overflow(name, error::ArithmeticOverflow::Underflow))?;
Ok(Value::Integer(v))
Since the value v is guaranteed to be negative after the negation step, subtracting 1 cannot cause an underflow in i128. Therefore, the code can be simplified as:
let v = v - 1;
You are right, I think compiler can optimize it. https://rust.godbolt.org/z/aP7KG4dr6
I would accept a PR to simplify it. :)