You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
78: Fix unsound lifetime on LinuxI2CMessage r=eldruin a=kevinmehall
The following code currently compiles, but passes a dangling pointer to the kernel, which will write to deallocated memory.
```rust
extern crate i2cdev;
use i2cdev::core::*;
use i2cdev::linux::{LinuxI2CBus, LinuxI2CMessage};
const SLAVE_ADDR: u16 = 0x57;
fn main() {
let mut dev = LinuxI2CBus::new("/dev/i2c-1").unwrap();
let mut v = vec![0, 0, 0];
let mut msgs = [
LinuxI2CMessage::write(&[0x01]).with_address(SLAVE_ADDR),
LinuxI2CMessage::read(&mut v).with_address(SLAVE_ADDR),
];
drop(v);
// Now pointer in in the message is pointing to the deallocated Vec
dev.transfer(&mut msgs).unwrap();
}
```
The lifetime parameter on the type alias doesn't do anything ([arguably a rustc bug that this is not an error](rust-lang/rust#82365)). The internal `i2c_msg` type without a lifetime parameter of its own does not actually enforce that the borrowed buffer is still valid at the time it is passed to the ioctl.
Co-authored-by: Kevin Mehall <km@kevinmehall.net>
Co-authored-by: Diego Barrios Romero <eldruin@gmail.com>
0 commit comments