Skip to content

Commit bfe616f

Browse files
committed
Constructor should be unsafe.
1 parent 8ea0c0e commit bfe616f

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

uart8250/src/registers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ register_bitfields![
206206

207207
impl Registers {
208208
/// Constructs a new instance of the UART registers starting at the given base address.
209-
pub fn from_base_address(base_address: usize) -> &'static mut Self {
210-
unsafe { &mut *(base_address as *mut crate::registers::Registers) }
209+
pub unsafe fn from_base_address(base_address: usize) -> &'static mut Self {
210+
&mut *(base_address as *mut crate::registers::Registers)
211211
}
212212
}

uart8250/src/uart.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ pub struct MmioUart8250<'a> {
2121

2222
impl<'a> MmioUart8250<'a> {
2323
/// Creates a new UART.
24-
pub fn new(base_address: usize) -> Self {
24+
///
25+
/// # Safety
26+
///
27+
/// The given base address must point to the 8 MMIO control registers of an appropriate UART
28+
/// device, which must be mapped into the address space of the process as device memory and not
29+
/// have any other aliases.
30+
pub unsafe fn new(base_address: usize) -> Self {
2531
Self {
2632
reg: Registers::from_base_address(base_address),
2733
}
@@ -49,7 +55,13 @@ impl<'a> MmioUart8250<'a> {
4955
}
5056

5157
/// Sets a new base address for the UART.
52-
pub fn set_base_address(&mut self, base_address: usize) {
58+
///
59+
/// # Safety
60+
///
61+
/// The given base address must point to the 8 MMIO control registers of an appropriate UART
62+
/// device, which must be mapped into the address space of the process as device memory and not
63+
/// have any other aliases.
64+
pub unsafe fn set_base_address(&mut self, base_address: usize) {
5365
self.reg = Registers::from_base_address(base_address);
5466
}
5567

@@ -434,7 +446,7 @@ mod tests {
434446
// Create a fake UART using an in-memory buffer, and check that it is initialised as
435447
// expected.
436448
let mut fake_registers: [u8; 8] = [0xff; 8];
437-
let uart = MmioUart8250::new(&mut fake_registers as *mut u8 as usize);
449+
let uart = unsafe { MmioUart8250::new(&mut fake_registers as *mut u8 as usize) };
438450

439451
uart.init(11_059_200, 115200);
440452

@@ -447,7 +459,7 @@ mod tests {
447459
#[test]
448460
fn write() {
449461
let mut fake_registers: [u8; 8] = [0; 8];
450-
let uart = MmioUart8250::new(&mut fake_registers as *mut u8 as usize);
462+
let uart = unsafe { MmioUart8250::new(&mut fake_registers as *mut u8 as usize) };
451463
uart.init(11_059_200, 115200);
452464

453465
uart.write_byte(0x42);
@@ -458,7 +470,7 @@ mod tests {
458470
#[test]
459471
fn read() {
460472
let mut fake_registers: [u8; 8] = [0; 8];
461-
let uart = MmioUart8250::new(&mut fake_registers as *mut u8 as usize);
473+
let uart = unsafe { MmioUart8250::new(&mut fake_registers as *mut u8 as usize) };
462474
uart.init(11_059_200, 115200);
463475

464476
// First try to read when there is nothing available.

0 commit comments

Comments
 (0)