Skip to content

Instantly share code, notes, and snippets.

@jamesmunns
Last active July 8, 2021 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmunns/71238a5ac4ff7c10e626471249c2e68e to your computer and use it in GitHub Desktop.
Save jamesmunns/71238a5ac4ff7c10e626471249c2e68e to your computer and use it in GitHub Desktop.
use rkyv::{
archived_root,
ser::{Serializer, serializers::AllocSerializer},
AlignedVec, Archive, Deserialize, Infallible, Serialize,
};
#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
enum Foo {
Bib,
Bim,
Bap,
}
#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
struct Test {
int: u8,
string: String,
option: Option<Vec<i32>>,
foo: Foo,
}
fn main() {
let value = Test {
int: 42,
string: "hello world".to_string(),
option: Some(vec![1, 2, 3, 4]),
foo: Foo::Bim,
};
let mut serializer = AllocSerializer::<4096>::default();
serializer.serialize_value(&value).unwrap();
let mut buf = serializer.into_serializer().into_inner();
println!("{:?}", buf);
let len = buf.len();
// Corrupt the enum - causes unexpected match
buf[len - 3] = 127;
// Corrupt the length - causes array out of index
// and a double panic
buf[32] = 0xFF;
println!("{:?}", buf);
let archived = unsafe { archived_root::<Test>(buf.as_slice()) };
assert_eq!(archived.int, value.int);
assert_eq!(archived.string, value.string);
assert_eq!(archived.option, value.option);
let deserialized: Test = archived.deserialize(&mut Infallible).unwrap();
match deserialized.foo {
Foo::Bib => println!("bib"),
Foo::Bim => println!("bim"),
Foo::Bap => println!("bap"),
_ => println!("wat"),
}
assert_eq!(value, deserialized);
println!("yay");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment