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
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:

include:
# Test MSRV
- rust: 1.60.0
- rust: 1.65.0
TARGET: x86_64-unknown-linux-gnu

# Test nightly but don't fail
Expand Down Expand Up @@ -53,7 +53,7 @@ jobs:

strategy:
matrix:
rust: [stable, 1.60.0]
rust: [stable, 1.65.0]
TARGET: [x86_64-apple-darwin]

steps:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

- Prevent underflow panics when using a `MockI2CDevice` with an offset of `0x0`
- Bumps the MSRV to 1.65.0

## [v0.6.0] - 2023-08-03

- Hide nix from the public api such that it can be updated without resulting in a breaking change.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ cross-compile. See <https://github.com/cross-rs/cross> for pointers.

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.60.0 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.65.0 and up. It *might*
compile with older versions but that may change in any new patch release.

## License
Expand Down
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();
}
}