278: Cyccnt r=texitoi a=perlindgren

The subtractions in `elapsed` and `duration` may cause an overflow panic in debug mode. This is solved by using wrapping arithmetics.  


Co-authored-by: Per Lindgren <per.lindgren@ltu.se>
This commit is contained in:
bors[bot] 2019-11-18 20:07:06 +00:00 committed by GitHub
commit 8374fce0df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 3 deletions

View file

@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] ## [Unreleased]
## v0.5.1 - 2019-11-18
- Fixed arithmetic wrapping bug in src/cyccntr.rs
elapsed and duration could cause an internal overflow trap
on subtraction in debug mode.
## v0.5.0 - 2019-11-14 ## v0.5.0 - 2019-11-14
### Added ### Added

View file

@ -13,7 +13,7 @@ license = "MIT OR Apache-2.0"
name = "cortex-m-rtfm" name = "cortex-m-rtfm"
readme = "README.md" readme = "README.md"
repository = "https://github.com/rtfm-rs/cortex-m-rtfm" repository = "https://github.com/rtfm-rs/cortex-m-rtfm"
version = "0.5.0" version = "0.5.1"
[lib] [lib]
name = "rtfm" name = "rtfm"

View file

@ -38,12 +38,14 @@ impl Instant {
/// Returns the amount of time elapsed since this instant was created. /// Returns the amount of time elapsed since this instant was created.
pub fn elapsed(&self) -> Duration { pub fn elapsed(&self) -> Duration {
Instant::now() - *self let diff = Instant::now().inner.wrapping_sub(self.inner);
assert!(diff >= 0, "instant now is earlier than self");
Duration { inner: diff as u32 }
} }
/// Returns the amount of time elapsed from another instant to this one. /// Returns the amount of time elapsed from another instant to this one.
pub fn duration_since(&self, earlier: Instant) -> Duration { pub fn duration_since(&self, earlier: Instant) -> Duration {
let diff = self.inner - earlier.inner; let diff = self.inner.wrapping_sub(earlier.inner);
assert!(diff >= 0, "second instant is later than self"); assert!(diff >= 0, "second instant is later than self");
Duration { inner: diff as u32 } Duration { inner: diff as u32 }
} }