forge
Explore
Log in

Don't you dare go Hollow

Prevent write-after-free by reordering Cast struct fields

Browse files at this commit

10df9f4717cbd4efd20ae796eb6b0aa400127bdcUnverifiedManuel Romei and Ivan MolodetskikhParent: 9f8eadc
  • fix
    • prevent write-after-free by reordering Cast struct fields
Show full message
fix(pw_utils): prevent write-after-free by reordering Cast struct fields

The Cast struct fields were ordered such that `stream` was dropped before
`_listener`. In Rust, struct fields are dropped in declaration order.

Because `StreamListener` attempts to unregister itself from the stream on
drop, and `StreamRc` destroys the underlying PipeWire stream on drop, the
previous order caused `_listener` to access the stream after it had
already been freed.

This reorders the fields so `_listener` is declared before `stream`,
ensuring the listener unregisters itself while the stream is still valid.

* Apply suggestion from @YaLTeR

---------
src/pw_utils.rsmodified+2 -1
@@ -72,8 +72,9 @@ pub struct Cast {
72 event_loop: LoopHandle<'static, State>,72 event_loop: LoopHandle<'static, State>,
73 pub session_id: usize,73 pub session_id: usize,
74 pub stream_id: usize,74 pub stream_id: usize,
75- pub stream: StreamRc,75+ // Listener is dropped before Stream to prevent a use-after-free.
76 _listener: StreamListener<()>,76 _listener: StreamListener<()>,
77+ pub stream: StreamRc,
77 pub target: CastTarget,78 pub target: CastTarget,
78 pub dynamic_target: bool,79 pub dynamic_target: bool,
79 formats: FormatSet,80 formats: FormatSet,