Skip to content

Instantly share code, notes, and snippets.

@djkoloski
Created July 8, 2021 23:20
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 djkoloski/e7472f9acda813b98d0b16c2c8be1ce9 to your computer and use it in GitHub Desktop.
Save djkoloski/e7472f9acda813b98d0b16c2c8be1ce9 to your computer and use it in GitHub Desktop.
use rkyv::{
check_archived_root,
ser::{Serializer, serializers::AllocSerializer},
AlignedVec, Archive, Deserialize, Infallible, Serialize,
};
#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
#[archive_attr(derive(bytecheck::CheckBytes))]
enum Foo {
Bib,
Bim,
Bap,
}
#[derive(Archive, Serialize, Deserialize, Debug, PartialEq)]
#[archive_attr(derive(bytecheck::CheckBytes))]
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 = check_archived_root::<Test>(buf.as_slice()).unwrap();
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