quininer / cbor4ii

CBOR: Concise Binary Object Representation
MIT License
54 stars 5 forks source link

serde: fix overflow on i64::MIN #12

Closed vmx closed 2 years ago

vmx commented 2 years ago

When decoding the minimum value of i64 (-9223372036854775808), there is an overflow error. With this fix it can be deserialized correctly.

The reason for the failure was the order of the operations. Prior to this change the decoding had these steps:

  1. Decode the bytes into a u64 => 9223372036854775807
  2. Add 1 => 9223372036854775808
  3. Cast to i64 => error as i64::MAX is 9223372036854775807.
  4. Negate the number

The new steps are:

  1. Decode the bytes into a u64 => 9223372036854775807
  2. Cast to i64 => 9223372036854775807
  3. Negate the number => -9223372036854775807
  4. Subtract 1 => -9223372036854775808
quininer commented 2 years ago

Thank you!