From eba4efef0ced7da6997803067874dbd390e859bc Mon Sep 17 00:00:00 2001 From: tilpner Date: Mon, 4 Sep 2023 11:31:10 +0200 Subject: [PATCH] fix: use explicit types for lifetime transmutation This fixes an accidental transmute (found by @projectgus) from an &Input to a &Context, which was only working accidentally by type layout coincidence. Co-Authored-By: Angus Gratton --- src/format/chapter/chapter_mut.rs | 4 ++-- src/format/context/common.rs | 4 ++-- src/format/context/input.rs | 8 ++++---- src/format/stream/stream_mut.rs | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/format/chapter/chapter_mut.rs b/src/format/chapter/chapter_mut.rs index ce28920..481516d 100644 --- a/src/format/chapter/chapter_mut.rs +++ b/src/format/chapter/chapter_mut.rs @@ -15,10 +15,10 @@ pub struct ChapterMut<'a> { impl<'a> ChapterMut<'a> { pub unsafe fn wrap(context: &mut Context, index: usize) -> ChapterMut<'_> { ChapterMut { - context: mem::transmute_copy(&context), + context: mem::transmute::<&mut Context, &mut Context>(context), index, - immutable: Chapter::wrap(mem::transmute_copy(&context), index), + immutable: Chapter::wrap(mem::transmute::<&Context, &Context>(context), index), } } diff --git a/src/format/context/common.rs b/src/format/context/common.rs index d3537d6..b480710 100644 --- a/src/format/context/common.rs +++ b/src/format/context/common.rs @@ -274,7 +274,7 @@ impl<'a> Iterator for StreamIterMut<'a> { unsafe { Some(StreamMut::wrap( - mem::transmute_copy(&self.context), + mem::transmute::<&mut Context, &mut Context>(self.context), (self.current - 1) as usize, )) } @@ -349,7 +349,7 @@ impl<'a> Iterator for ChapterIterMut<'a> { self.current += 1; Some(ChapterMut::wrap( - mem::transmute_copy(&self.context), + mem::transmute::<&mut Context, &mut Context>(self.context), (self.current - 1) as usize, )) } diff --git a/src/format/context/input.rs b/src/format/context/input.rs index 8d423bb..c2dbdf5 100644 --- a/src/format/context/input.rs +++ b/src/format/context/input.rs @@ -181,10 +181,10 @@ impl<'a> Iterator for PacketIter<'a> { Err(Error::Eof) => None, Ok(..) => unsafe { - Some(Ok(( - Stream::wrap(mem::transmute_copy(&self.context), packet.stream()), - packet, - ))) + let context = mem::transmute::<&Context, &Context>(self.context); + let stream = Stream::wrap(context, packet.stream()); + + Some(Ok((stream, packet))) }, Err(err) => Some(Err(err)), diff --git a/src/format/stream/stream_mut.rs b/src/format/stream/stream_mut.rs index 902df39..565f323 100644 --- a/src/format/stream/stream_mut.rs +++ b/src/format/stream/stream_mut.rs @@ -13,10 +13,10 @@ pub struct StreamMut<'a> { impl<'a> StreamMut<'a> { pub unsafe fn wrap(context: &mut Context, index: usize) -> StreamMut<'_> { StreamMut { - context: mem::transmute_copy(&context), + context: mem::transmute::<&mut Context, &mut Context>(context), index, - immutable: Stream::wrap(mem::transmute_copy(&context), index), + immutable: Stream::wrap(mem::transmute::<&Context, &Context>(context), index), } }