Skip to content

Commit 28d12e9

Browse files
authored
add missing Send + 'static bounds to EventWatchCallback and AudioCallback to avoid soundness hole (#1509)
* add Sync + Send bounds to EventWatchCallback to avoid soundness hole * add 'static bounds to EventWatchCallback * update changelog * add 'static to AudioCallback too * remove unnecessary Sync bounds on EventWatchCallback and lifetime param on EventWatch * fix formatting * update changelog to reflect AudioCallback change
1 parent adeb978 commit 28d12e9

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ when upgrading from a version of rust-sdl2 to another.
33

44
### v0.39.0
55

6+
[PR #1509](https://github.com/Rust-SDL2/rust-sdl2/pull/1509) **BREAKING CHANGE** Add Send + 'static bounds for EventWatchCallback and 'static bound to AudioCallback to avoid soundness hole.
7+
68
[PR #1506](https://github.com/Rust-SDL2/rust-sdl2/pull/1506) **BREAKING CHANGE** Update crates.io dependencies, update bundled SDL2 to 2.32.10, add whitelist to bindgen to only emit SDL related items, and specifically no platform (or compiler, etc) specific items. Implement the same set of useful derived traits on bitflags types.
79

810
### v0.38.0

src/sdl2/audio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl Drop for AudioSpecWAV {
512512
}
513513
}
514514

515-
pub trait AudioCallback: Send
515+
pub trait AudioCallback: Send + 'static
516516
where
517517
Self::Channel: AudioFormatNum + 'static,
518518
{

src/sdl2/event.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,7 @@ impl crate::EventSubsystem {
246246
/// dbg!(event);
247247
/// });
248248
/// ```
249-
pub fn add_event_watch<'a, CB: EventWatchCallback + 'a>(
250-
&self,
251-
callback: CB,
252-
) -> EventWatch<'a, CB> {
249+
pub fn add_event_watch<CB: EventWatchCallback>(&self, callback: CB) -> EventWatch<CB> {
253250
EventWatch::add(callback)
254251
}
255252
}
@@ -2955,26 +2952,24 @@ impl EventSender {
29552952
}
29562953

29572954
/// A callback trait for [`EventSubsystem::add_event_watch`].
2958-
pub trait EventWatchCallback {
2955+
pub trait EventWatchCallback: Send + 'static {
29592956
fn callback(&mut self, event: Event);
29602957
}
29612958

29622959
/// An handler for the event watch callback.
29632960
/// One must bind this struct in a variable as long as you want to keep the callback active.
29642961
/// For further information, see [`EventSubsystem::add_event_watch`].
2965-
pub struct EventWatch<'a, CB: EventWatchCallback + 'a> {
2962+
pub struct EventWatch<CB: EventWatchCallback> {
29662963
activated: bool,
29672964
callback: Box<CB>,
2968-
_phantom: PhantomData<&'a CB>,
29692965
}
29702966

2971-
impl<'a, CB: EventWatchCallback + 'a> EventWatch<'a, CB> {
2972-
fn add(callback: CB) -> EventWatch<'a, CB> {
2967+
impl<CB: EventWatchCallback> EventWatch<CB> {
2968+
fn add(callback: CB) -> EventWatch<CB> {
29732969
let f = Box::new(callback);
29742970
let mut watch = EventWatch {
29752971
activated: false,
29762972
callback: f,
2977-
_phantom: PhantomData,
29782973
};
29792974
watch.activate();
29802975
watch
@@ -3021,7 +3016,7 @@ impl<'a, CB: EventWatchCallback + 'a> EventWatch<'a, CB> {
30213016
}
30223017
}
30233018

3024-
impl<'a, CB: EventWatchCallback + 'a> Drop for EventWatch<'a, CB> {
3019+
impl<CB: EventWatchCallback> Drop for EventWatch<CB> {
30253020
fn drop(&mut self) {
30263021
self.deactivate();
30273022
}
@@ -3037,7 +3032,7 @@ extern "C" fn event_callback_marshall<CB: EventWatchCallback>(
30373032
0
30383033
}
30393034

3040-
impl<F: FnMut(Event)> EventWatchCallback for F {
3035+
impl<F: FnMut(Event) + Send + 'static> EventWatchCallback for F {
30413036
fn callback(&mut self, event: Event) {
30423037
self(event)
30433038
}

0 commit comments

Comments
 (0)