Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UB in BorshDeserialize for types that have validity invariants #18

Closed
dtolnay opened this issue Mar 13, 2021 · 1 comment · Fixed by #21 or #26
Closed

UB in BorshDeserialize for types that have validity invariants #18

dtolnay opened this issue Mar 13, 2021 · 1 comment · Fixed by #21 or #26
Assignees
Labels
bug Something isn't working

Comments

@dtolnay
Copy link
Contributor

dtolnay commented Mar 13, 2021

Repro that ends up executing SIGILL due to undefined behavior:

use borsh::BorshDeserialize;
use std::io::Result;

struct B(u8, Void);
enum Void {}

impl BorshDeserialize for B {
    fn deserialize(_buf: &mut &[u8]) -> Result<Self> {
        unreachable!()
    }
    fn is_u8() -> bool {
        true
    }
}

fn main() {
    let x = Vec::<B>::deserialize(&mut &[1, 0, 0, 0, 0][..]).unwrap();
    match x[0].1 {}
}
$ cargo run --release
Illegal instruction (core dumped)
@frol
Copy link
Collaborator

frol commented Mar 15, 2021

@evgenykuzyakov It seems that we should mark is_u8 as unsafe and make sure we have the documentation on why it is so.

@dtolnay Do you think there is a better approach?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants