Skip to content

Commit bd5f200

Browse files
authored
Merge pull request #83 from chiefnoah/master
Prevent underflow when using a MockI2CDevice with a RegisterMap.offset = 0x0
2 parents 594860e + 8447d41 commit bd5f200

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222

2323
include:
2424
# Test MSRV
25-
- rust: 1.60.0
25+
- rust: 1.65.0
2626
TARGET: x86_64-unknown-linux-gnu
2727

2828
# Test nightly but don't fail
@@ -53,7 +53,7 @@ jobs:
5353

5454
strategy:
5555
matrix:
56-
rust: [stable, 1.60.0]
56+
rust: [stable, 1.65.0]
5757
TARGET: [x86_64-apple-darwin]
5858

5959
steps:

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
- Prevent underflow panics when using a `MockI2CDevice` with an offset of `0x0`
12+
- Bumps the MSRV to 1.65.0
13+
1114
## [v0.6.0] - 2023-08-03
1215

1316
- Hide nix from the public api such that it can be updated without resulting in a breaking change.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ cross-compile. See <https://github.com/cross-rs/cross> for pointers.
5454

5555
## Minimum Supported Rust Version (MSRV)
5656

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

6060
## License

src/mock.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88
use core::{I2CDevice, I2CMessage, I2CTransfer};
9+
use std::convert::TryFrom;
910
use std::io;
1011

1112
/// I2C mock result type
@@ -44,7 +45,12 @@ impl I2CRegisterMap {
4445
fn read(&mut self, data: &mut [u8]) -> I2CResult<()> {
4546
let len = data.len();
4647
data.clone_from_slice(&self.registers[self.offset..(self.offset + len)]);
47-
println!("READ | 0x{:X} : {:?}", self.offset - data.len(), data);
48+
println!(
49+
"READ | 0x{:X} : {:?}",
50+
isize::try_from(self.offset).unwrap_or(0xBAD)
51+
- isize::try_from(data.len()).unwrap_or(0xBAD),
52+
data
53+
);
4854
Ok(())
4955
}
5056

@@ -156,3 +162,15 @@ where
156162
Ok(messages.len() as u32)
157163
}
158164
}
165+
166+
#[cfg(test)]
167+
mod test {
168+
use super::*;
169+
170+
#[test]
171+
fn test_can_read_at_zero_offset() {
172+
let mut mock_device = MockI2CDevice::new();
173+
mock_device.regmap.write_regs(0x0, &[0x1u8; 4]);
174+
mock_device.read(&mut [0x0u8; 4]).unwrap();
175+
}
176+
}

0 commit comments

Comments
 (0)