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

Prevent underflow when using a MockI2CDevice with a RegisterMap.offset = 0x0 #83

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use core::{I2CDevice, I2CMessage, I2CTransfer};
use std::convert::TryFrom;
use std::io;

/// I2C mock result type
Expand Down Expand Up @@ -44,7 +45,12 @@ impl I2CRegisterMap {
fn read(&mut self, data: &mut [u8]) -> I2CResult<()> {
let len = data.len();
data.clone_from_slice(&self.registers[self.offset..(self.offset + len)]);
println!("READ | 0x{:X} : {:?}", self.offset - data.len(), data);
println!(
"READ | 0x{:X} : {:?}",
isize::try_from(self.offset).unwrap_or(0xBAD)
- isize::try_from(data.len()).unwrap_or(0xBAD),
data
);
Ok(())
}

Expand Down Expand Up @@ -156,3 +162,15 @@ where
Ok(messages.len() as u32)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_can_read_at_zero_offset() {
let mut mock_device = MockI2CDevice::new();
mock_device.regmap.write_regs(0x0, &[0x1u8; 4]);
mock_device.read(&mut [0x0u8; 4]).unwrap();
}
}
Loading