Skip to content

Commit

Permalink
fix: binary body lifetime issue
Browse files Browse the repository at this point in the history
There's a subtle lifetime issue created in the original code. The
original code uses:

- `Bytes::from(&'static [u8])`
- `from_raw_parts<'a, T>(*const T, usize) -> &'a [T]`

Which requires the `*const T` pointer be a static pointer. Being
provided through the FFI, this is unreasonable, and we should expect it
to last only as long as the function call itself.

By simply replacing `Bytes::from` with `Bytes::from_from_slice`, we
narrow down the lifetime from `'static` to `'a`, allowing for the
underlying `*const T` pointer to be deallocated once the function has
returned.

Signed-off-by: JP-Ellis <josh@jpellis.me>
  • Loading branch information
JP-Ellis committed Mar 12, 2024
1 parent 72f8368 commit fca0c4a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion rust/pact_ffi/src/mock_server/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,7 @@ fn convert_ptr_to_body(body: *const u8, size: size_t, content_type: Option<Conte
} else if size == 0 {
OptionalBody::Empty
} else {
OptionalBody::Present(Bytes::from(unsafe { std::slice::from_raw_parts(body, size) }), content_type, None)
OptionalBody::Present(Bytes::copy_from_slice(unsafe { std::slice::from_raw_parts(body, size) }), content_type, None)
}
}

Expand Down

0 comments on commit fca0c4a

Please sign in to comment.