Skip to content

Commit

Permalink
fix: don't perform invalid reference cast
Browse files Browse the repository at this point in the history
Replaces it with a raw pointer. Avoids possible Undefined Behavior.
  • Loading branch information
wackbyte committed Jan 31, 2024
1 parent 7a99ba6 commit bff060e
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions crates/mun_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,6 @@ impl<'name, T: InvokeArgs> InvokeErr<'name, T> {
Output: 'o + ReturnTypeReflection + Marshal<'o>,
'r: 'o,
{
// Safety: The output of `retry_impl` is guaranteed to only contain a shared
// reference.
let runtime = &*runtime;

loop {
self = match unsafe { self.retry_impl(runtime) } {
Ok(output) => return output,
Expand All @@ -687,15 +683,14 @@ impl<'name, T: InvokeArgs> InvokeErr<'name, T> {
///
/// # Safety
///
/// When calling this function, you have to guarantee that `runtime` is mutably
/// borrowed. The `Output` value can only contain a shared borrow of `runtime`.
unsafe fn retry_impl<'r, 'o, Output>(self, runtime: &'r Runtime) -> Result<Output, Self>
/// When calling this function, you have to guarantee that `runtime` can be dereferenced and is
/// valid for `'o`. The `Output` value can only contain a shared borrow of `runtime`.
unsafe fn retry_impl<'o, Output>(self, runtime: *mut Runtime) -> Result<Output, Self>

Check warning on line 688 in crates/mun_runtime/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/mun_runtime/src/lib.rs#L688

Added line #L688 was not covered by tests
where
Output: 'o + ReturnTypeReflection + Marshal<'o>,
'r: 'o,
{
#[allow(invalid_reference_casting, invalid_reference_casting)]
let runtime = &mut *(runtime as *const Runtime as *mut Runtime);
// Safety: Guaranteed by the caller to be valid to dereference.
let runtime = &mut *runtime;

Check warning on line 693 in crates/mun_runtime/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/mun_runtime/src/lib.rs#L692-L693

Added lines #L692 - L693 were not covered by tests

eprintln!("{}", self.msg);
while !runtime.update() {
Expand Down

0 comments on commit bff060e

Please sign in to comment.