Skip to content

Repository files navigation

bytecheck Latest VersionLicenserequires: rustc 1.52+

bytecheck is a type validation framework for Rust.

bytecheck in action

use bytecheck::CheckBytes;#[derive(CheckBytes,Debug)]structTest{a:u32,b:bool,c:char,}#[repr(C, align(16))]structAligned<constN:usize>([u8;N]);macro_rules! bytes {($($byte:literal,)*) => {(&Aligned([$($byte,)*]).0as&[u8]).as_ptr()};($($byte:literal),*) => {
bytes!($($byte,)*)};}fnmain(){// This type is laid out as (u32, char, bool)unsafe{// These are valid bytes for (0, 'x', true)Test::check_bytes(bytes![0u8,0u8,0u8,0u8,0x78u8,0u8,0u8,0u8,1u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8].cast(),&()).unwrap();// Changing the bytes for the u32 is OK, any bytes are a valid u32Test::check_bytes(bytes![42u8,16u8,20u8,3u8,0x78u8,0u8,0u8,0u8,1u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8].cast(),&()).unwrap();// Characters outside the valid ranges are invalidTest::check_bytes(bytes![0u8,0u8,0u8,0u8,0x00u8,0xd8u8,0u8,0u8,1u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8].cast(),&()).unwrap_err();Test::check_bytes(bytes![0u8,0u8,0u8,0u8,0x00u8,0x00u8,0x11u8,0u8,1u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8].cast(),&()).unwrap_err();// 0 is a valid boolean value (false) but 2 is notTest::check_bytes(bytes![0u8,0u8,0u8,0u8,0x78u8,0u8,0u8,0u8,0u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8].cast(),&()).unwrap();Test::check_bytes(bytes![0u8,0u8,0u8,0u8,0x78u8,0u8,0u8,0u8,2u8,255u8,255u8,255u8,255u8,255u8,255u8,255u8].cast(),&()).unwrap_err();}}

About

Memory validation framework for Rust

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages