Skip to content

Commit 6213961

Browse files
committed
zephyr: device: uart: Fix lifetimes of 'inner' method on UartIrq
This method had a lifetime error, and allowed the inner uart to be leaked beyond its lifetime. This should prevent the returned device from outliving the UartIrq, or even being used after mutable methods on the UartIrq have been used. For now, don't even return a mutable method until we evaluate what should be permissible. This at least allows reading the line status. In addition, since the return Uart is not owned by the caller, it is not permissible to call any of the `into_*` methods. Signed-off-by: David Brown <[email protected]>
1 parent b91ba47 commit 6213961

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

zephyr/src/device/uart.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ pub struct UartIrq {
209209
device: *const raw::device,
210210
/// Critical section protected data.
211211
data: Arc<IrqOuterData>,
212+
/// Interior wrapped device, to be able to hand out lifetime managed references to it.
213+
uart: Uart,
212214
}
213215

214216
// UartIrq is also Send, !Sync, for the same reasons as for Uart.
@@ -243,14 +245,16 @@ impl UartIrq {
243245
Ok(UartIrq {
244246
device: uart.device,
245247
data,
248+
uart,
246249
})
247250
}
248251

249252
/// Get the underlying UART to be able to change line control and such.
250-
pub unsafe fn inner(&mut self) -> Uart {
251-
Uart {
252-
device: self.device
253-
}
253+
///
254+
/// TODO: This really should return something like `&Uart` to bind the lifetime. Otherwise the
255+
/// user can continue to use the uart handle beyond the lifetime of the driver.
256+
pub unsafe fn inner(&mut self) -> &Uart {
257+
&self.uart
254258
}
255259

256260
/// Attempt to read data from the UART into the buffer. If no data is available, it will

0 commit comments

Comments
 (0)