secrets
secrets copied to clipboard
`bool` and `char`'s `Bytes` implementations cause undefined behavior.
The documentation for secrets::traits::Bytes states:
Any type that implements Bytes must not exhibit undefined behavior when its underlying bits are set to any arbitrary bit pattern.
Currently, bool and char (the primitive types) have implementations for Bytes (https://github.com/stouset/secrets/blob/master/src/traits.rs#L69 ), but these types can not be set to arbitrary bit patterns (specifically, bool must have the bit pattern 0x00 or 0x01, and char must have a bit pattern in the range 0x0000_0000..=0x0000_D7FF or the range 0x0000_E000..=0x0010_FFFF)
The following example program exhibits undefined behavior due to this (run it in debug mode and in release mode and you'll most likely see different results):
fn main() {
let b: char = secrets::traits::Bytes::uninitialized();
match b {
// Note that these two patterns together include all valid char values
'\x00'..='\u{10fffe}' => dbg!("char1"),
'\x01'..='\u{10ffff}' => dbg!("char2"), // This prints in release mode on my machine (secrets 1.2.0, rustc 1.61.0)
_ => dbg!("huh?"), // This prints in debug mode on my machine
};
}