Skip to content

Commit

Permalink
Merge #225
Browse files Browse the repository at this point in the history
225: Avoid creating intermediate references to packed fields r=richardeoin a=richardeoin

Such an intermediate reference is UB, even if it is never dereferenced. The previous code that created addresses / raw pointers via intermediate references will become a hard error in a future version of Rust. Use [core::ptr::addr_of](https://doc.rust-lang.org/core/ptr/macro.addr_of.html) instead.

This PR increases the MSRV to 1.51.0

Co-authored-by: Richard Meadows <962920+richardeoin@users.noreply.github.com>
  • Loading branch information
bors[bot] and richardeoin authored Aug 2, 2021
2 parents bcd1f0b + c10c266 commit 797efe5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
matrix: # All permutations of {rust, mcu}
rust:
- 1.43.0 # MSRV
- 1.51.0 # MSRV
- stable
mcu:
- stm32h743
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

* MSRV increased to 1.51.0

## [v0.10.0] 2021-07-xx

* **Breaking**: Don't reset peripheral in DMA1/2 `StreamsTuple::new()` method #229
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ programming interfaces are only available on the high density connectors.
Minimum supported Rust version
------------------------------

The Minimum Supported Rust Version (MSRV) at the moment is **1.43.0**. Older
The Minimum Supported Rust Version (MSRV) at the moment is **1.51.0**. Older
versions **may** compile, especially when some features are not used
in your application.

Expand Down
19 changes: 9 additions & 10 deletions src/ethernet/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
//! [quartiq/stabilizer]: https://github.com/quartiq/stabilizer
//! [notes]: https://github.com/quartiq/stabilizer/commit/ab1735950b2108eaa8d51eb63efadcd2e25c35c4

use core::ptr;

use crate::rcc::{rec, CoreClocks, ResetEnable};
use crate::stm32;

Expand Down Expand Up @@ -147,9 +149,7 @@ impl TDesRing {
let x = self.tdidx;
assert!(self.td[x].tdes3 & EMAC_DES3_OWN == 0); // Owned by us

// unsafe: tbuf is actually aligned, but with repr(packed) the
// compiler cannot infer this
let address = unsafe { self.tbuf[x].as_ptr() as u32 };
let address = ptr::addr_of!(self.tbuf[x]) as u32;

// Read format
self.td[x].tdes0 = address; // Buffer 1
Expand Down Expand Up @@ -181,14 +181,15 @@ impl TDesRing {
let x = self.tdidx;

// Set address in descriptor
self.td[x].tdes0 = self.tbuf[x].as_ptr() as u32; // Buffer 1
self.td[x].tdes0 = ptr::addr_of!(self.tbuf[x]) as u32; // Buffer 1

// Set length in descriptor
let len = core::cmp::min(length, ETH_BUF_SIZE);
self.td[x].tdes2 = (length as u32) & EMAC_TDES2_B1L;

// Return buffer slice
let addr = self.tbuf[x].as_ptr() as *mut _;
// Create a raw pointer in place without an intermediate reference. Use
// this to return a slice from the packed buffer
let addr = ptr::addr_of_mut!(self.tbuf[x]) as *mut _;
core::slice::from_raw_parts_mut(addr, len)
}
}
Expand Down Expand Up @@ -301,9 +302,7 @@ impl RDesRing {
let x = self.rdidx;
assert!(self.rd[x].rdes3 & EMAC_DES3_OWN == 0); // Owned by us

// unsafe: rbuf is actually aligned, but with repr(packed) the
// compiler cannot infer this
let address = unsafe { self.rbuf[x].as_ptr() as u32 };
let address = ptr::addr_of!(self.rbuf[x]) as u32;

// Read format
self.rd[x].rdes0 = address; // Buffer 1
Expand Down Expand Up @@ -340,7 +339,7 @@ impl RDesRing {
let x = self.rdidx;

// Write-back format
let addr = self.rbuf[x].as_ptr() as *mut u8;
let addr = ptr::addr_of!(self.rbuf[x]) as *mut u8;
let len = (self.rd[x].rdes3 & EMAC_RDES3_PL) as usize;

let len = core::cmp::min(len, ETH_BUF_SIZE);
Expand Down

0 comments on commit 797efe5

Please sign in to comment.