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 <gus@projectgus.com>
This commit is contained in:
tilpner
2023-09-04 14:15:38 +02:00
committed by Till Höppner
co-authored by Angus Gratton
parent 10545c00d4
commit eba4efef0c
4 changed files with 10 additions and 10 deletions
+2 -2
View File
@@ -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),
}
}
+2 -2
View File
@@ -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,
))
}
+4 -4
View File
@@ -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)),
+2 -2
View File
@@ -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),
}
}