Skip to content

fix: binary body lifetime issue#400

Merged
rholshausen merged 1 commit intomasterfrom
fix/lifetime
Mar 12, 2024
Merged

fix: binary body lifetime issue#400
rholshausen merged 1 commit intomasterfrom
fix/lifetime

Conversation

@JP-Ellis
Copy link
Copy Markdown
Contributor

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.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants