Skip to content

Commit

Permalink
Fix UB: Non-static callbacks are unsound
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Nov 11, 2023
1 parent 5948ab3 commit d178c96
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ pub trait EventBus<P>: ErrorType {
where
Self: 'a;

fn subscribe<'a, F>(&'a self, callback: F) -> Result<Self::Subscription<'a>, Self::Error>
fn subscribe<F>(&self, callback: F) -> Result<Self::Subscription<'_>, Self::Error>
where
F: FnMut(&P) + Send + 'a;
F: FnMut(&P) + Send + 'static;
}

impl<'e, P, E> EventBus<P> for &'e E
Expand All @@ -62,9 +62,9 @@ where
{
type Subscription<'a> = E::Subscription<'a> where Self: 'a;

fn subscribe<'a, F>(&'a self, callback: F) -> Result<Self::Subscription<'a>, Self::Error>
fn subscribe<F>(&self, callback: F) -> Result<Self::Subscription<'_>, Self::Error>
where
F: FnMut(&P) + Send + 'a,
F: FnMut(&P) + Send + 'static,
{
(**self).subscribe(callback)
}
Expand All @@ -76,9 +76,9 @@ where
{
type Subscription<'a> = E::Subscription<'a> where Self: 'a;

fn subscribe<'a, F>(&'a self, callback: F) -> Result<Self::Subscription<'a>, Self::Error>
fn subscribe<F>(&self, callback: F) -> Result<Self::Subscription<'_>, Self::Error>
where
F: FnMut(&P) + Send + 'a,
F: FnMut(&P) + Send + 'static,
{
(**self).subscribe(callback)
}
Expand Down
8 changes: 4 additions & 4 deletions src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub trait Ping {

fn ping(&mut self, ip: ipv4::Ipv4Addr, conf: &Configuration) -> Result<Summary, Self::Error>;

fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand All @@ -81,7 +81,7 @@ where
(*self).ping(ip, conf)
}

fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand All @@ -107,7 +107,7 @@ pub mod asynch {
conf: &Configuration,
) -> Result<Summary, Self::Error>;

async fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
async fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand All @@ -129,7 +129,7 @@ pub mod asynch {
(*self).ping(ip, conf).await
}

async fn ping_details<F: FnMut(&Summary, &Reply) + Send>(
async fn ping_details<F: FnMut(&Summary, &Reply) + Send + 'static>(
&mut self,
ip: ipv4::Ipv4Addr,
conf: &Configuration,
Expand Down
12 changes: 6 additions & 6 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ pub trait TimerService: ErrorType {
where
Self: 'a;

fn timer<'a, F>(&'a self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
fn timer<F>(&self, callback: F) -> Result<Self::Timer<'_>, Self::Error>
where
F: FnMut() + Send + 'a;
F: FnMut() + Send + 'static;
}

impl<S> TimerService for &S
Expand All @@ -83,9 +83,9 @@ where
{
type Timer<'a> = S::Timer<'a> where Self: 'a;

fn timer<'a, F>(&'a self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
fn timer<F>(&self, callback: F) -> Result<Self::Timer<'_>, Self::Error>
where
F: FnMut() + Send + 'a,
F: FnMut() + Send + 'static,
{
(*self).timer(callback)
}
Expand All @@ -97,9 +97,9 @@ where
{
type Timer<'a> = S::Timer<'a> where Self: 'a;

fn timer<'a, F>(&'a self, callback: F) -> Result<Self::Timer<'a>, Self::Error>
fn timer<F>(&self, callback: F) -> Result<Self::Timer<'_>, Self::Error>
where
F: FnMut() + Send + 'a,
F: FnMut() + Send + 'static,
{
(**self).timer(callback)
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/asyncify/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ where
where
P: Clone + Send + 'static,
E: crate::event_bus::EventBus<P>,
for<'a> E::Subscription<'a>: Send,
for<'a> E::Subscription<'a>: Send + 'static,
{
let state = Arc::new((
Mutex::new(SubscriptionState {
Expand Down

0 comments on commit d178c96

Please sign in to comment.