Fix PeripheralRef soundness issue allowing &T.

Fix soundness issue introduced in a previous soundness fix https://github.com/embassy-rs/embassy/pull/2602 .
PeripheralRef must not implement DerefMut itself, but the blanket impl must still require DerefMut. Otherwise
you can create two instances of a driver on the same uart by using `&my_uart`.
This commit is contained in:
Dario Nieuwenhuis
2024-05-07 23:21:55 +02:00
parent 0f11fecff6
commit b13ad7e80b
2 changed files with 23 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
use core::marker::PhantomData;
use core::ops::Deref;
use core::ops::{Deref, DerefMut};
/// An exclusive reference to a peripheral.
///
@@ -155,7 +155,7 @@ pub trait Peripheral: Sized {
}
}
impl<'b, T: Deref> Peripheral for T
impl<'b, T: DerefMut> Peripheral for T
where
T::Target: Peripheral,
{
@@ -163,6 +163,15 @@ where
#[inline]
unsafe fn clone_unchecked(&self) -> Self::P {
self.deref().clone_unchecked()
T::Target::clone_unchecked(self)
}
}
impl<'b, T: Peripheral> Peripheral for PeripheralRef<'_, T> {
type P = T::P;
#[inline]
unsafe fn clone_unchecked(&self) -> Self::P {
T::clone_unchecked(self)
}
}