Skip to content

Commit

Permalink
Account for Bincode allowing trailing bytes by default (#607)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Jun 11, 2024
1 parent 876b125 commit 2b81eae
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/odd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,23 @@ mod tests {
assert_eq!(&error_message, "io error: unexpected end of file");
}

// @reviewers: I expected an error when deserializing an Odd<U128> into an Odd<U64>, instead I get a truncated number. Is this a big or known limitation?
#[test]
fn silently_coerces_bigger_type_into_smaller_type() {
let three = Odd::new(U128::from_u128(0x77777777777777773333333333333333)).unwrap();
use bincode::Options;

let three_ser = bincode::serialize(&three).unwrap();
// Use custom options to disallow trailing bytes.
let options = bincode::DefaultOptions::new();

// This doesn't fail, which is unexpected
let smaller = bincode::deserialize::<Odd<U64>>(&three_ser).unwrap();
let three = Odd::new(U128::from_u128(0x77777777777777773333333333333333)).unwrap();
let three_ser = options.serialize(&three).unwrap();
let error_message = options
.deserialize::<Odd<U64>>(&three_ser)
.unwrap_err()
.to_string();

assert_eq!(
smaller,
Odd::new(U64::from_u64(0x3333333333333333)).unwrap()
&error_message,
"Slice had bytes remaining after deserialization"
);
}
}
Expand Down

0 comments on commit 2b81eae

Please sign in to comment.