Skip to content

Commit

Permalink
fix(ffi): add repr(transparent) to hyper_context (hyperium#3191)
Browse files Browse the repository at this point in the history
The default representation does not guarantee the absence of initial
padding, which is necessary for the transmute to be sound.

Signed-off-by: Sven Pfennig <s.pfennig@reply.de>
  • Loading branch information
LegionMammal978 authored and 0xE282B0 committed Jan 16, 2024
1 parent c9f80fc commit dfb0adc
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ffi/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct TaskFuture {
}

/// An async context for a task that contains the related waker.
#[repr(transparent)]
pub struct hyper_context<'a>(Context<'a>);

/// A waker that is saved and used to waken a pending task.
Expand Down Expand Up @@ -378,7 +379,7 @@ where

impl hyper_context<'_> {
pub(crate) fn wrap<'a, 'b>(cx: &'a mut Context<'b>) -> &'a mut hyper_context<'b> {
// A struct with only one field has the same layout as that field.
// A repr(transparent) struct with only one field has the same layout as that field.
unsafe { std::mem::transmute::<&mut Context<'_>, &mut hyper_context<'_>>(cx) }
}
}
Expand Down

0 comments on commit dfb0adc

Please sign in to comment.