mirror of
https://github.com/esp-rs/esp-idf-hal.git
synced 2025-09-30 13:50:53 +00:00
Fix UB: Non-static callbacks are unsound
This commit is contained in:
parent
f917d5302b
commit
79fc6e738b
@ -1147,17 +1147,18 @@ impl<'d, T: Pin, MODE> PinDriver<'d, T, MODE> {
|
|||||||
/// Care should be taken not to call STD, libc or FreeRTOS APIs (except for a few allowed ones)
|
/// Care should be taken not to call STD, libc or FreeRTOS APIs (except for a few allowed ones)
|
||||||
/// in the callback passed to this function, as it is executed in an ISR context.
|
/// in the callback passed to this function, as it is executed in an ISR context.
|
||||||
#[cfg(all(not(feature = "riscv-ulp-hal"), feature = "alloc"))]
|
#[cfg(all(not(feature = "riscv-ulp-hal"), feature = "alloc"))]
|
||||||
pub unsafe fn subscribe(&mut self, callback: impl FnMut() + Send + 'd) -> Result<(), EspError>
|
pub unsafe fn subscribe<F>(&mut self, callback: F) -> Result<(), EspError>
|
||||||
where
|
where
|
||||||
|
F: FnMut() + Send + 'static,
|
||||||
MODE: InputMode,
|
MODE: InputMode,
|
||||||
{
|
{
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
self.disable_interrupt()?;
|
self.disable_interrupt()?;
|
||||||
|
|
||||||
let callback: alloc::boxed::Box<dyn FnMut() + Send + 'd> = alloc::boxed::Box::new(callback);
|
let callback: alloc::boxed::Box<dyn FnMut() + Send + 'static> =
|
||||||
chip::PIN_ISR_HANDLER[self.pin.pin() as usize] =
|
alloc::boxed::Box::new(callback);
|
||||||
Some(unsafe { core::mem::transmute(callback) });
|
chip::PIN_ISR_HANDLER[self.pin.pin() as usize] = Some(callback);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -459,9 +459,9 @@ impl<'d> PcntDriver<'d> {
|
|||||||
/// - ()
|
/// - ()
|
||||||
/// - EspError
|
/// - EspError
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
pub unsafe fn subscribe<C>(&self, callback: C) -> Result<(), EspError>
|
pub unsafe fn subscribe<F>(&self, callback: F) -> Result<(), EspError>
|
||||||
where
|
where
|
||||||
C: FnMut(u32) + Send + 'static,
|
F: FnMut(u32) + Send + 'static,
|
||||||
{
|
{
|
||||||
enable_isr_service()?;
|
enable_isr_service()?;
|
||||||
|
|
||||||
|
@ -287,15 +287,18 @@ impl<'d> TimerDriver<'d> {
|
|||||||
/// Care should be taken not to call STD, libc or FreeRTOS APIs (except for a few allowed ones)
|
/// Care should be taken not to call STD, libc or FreeRTOS APIs (except for a few allowed ones)
|
||||||
/// in the callback passed to this function, as it is executed in an ISR context.
|
/// in the callback passed to this function, as it is executed in an ISR context.
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
pub unsafe fn subscribe(&mut self, callback: impl FnMut() + Send + 'd) -> Result<(), EspError> {
|
pub unsafe fn subscribe<F>(&mut self, callback: F) -> Result<(), EspError>
|
||||||
|
where
|
||||||
|
F: FnMut() + Send + 'static,
|
||||||
|
{
|
||||||
self.check();
|
self.check();
|
||||||
|
|
||||||
self.disable_interrupt()?;
|
self.disable_interrupt()?;
|
||||||
|
|
||||||
let callback: Box<dyn FnMut() + Send + 'd> = Box::new(callback);
|
let callback: Box<dyn FnMut() + Send + 'static> = Box::new(callback);
|
||||||
|
|
||||||
ISR_HANDLERS[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize] =
|
ISR_HANDLERS[(self.group() * timer_idx_t_TIMER_MAX + self.index()) as usize] =
|
||||||
Some(unsafe { core::mem::transmute(callback) });
|
Some(callback);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user