Skip to content

Commit

Permalink
ndk/media_codec: Return MaybeUninit bytes in buffer_mut() (#403)
Browse files Browse the repository at this point in the history
The byte-array mapped/returned by `AMediaCodec_getInputBuffer()` is not
known to be initialized as the caller requires this API to subsequently
initialize the data, and mapping/casting uninitialized memory segments
as safe data in Rust is Undefined Behaviour.  Instead, return a
`MaybeUninit<u8>` type to be safe (as in: the user cannot *safely* read
data in the returned slice, only write to it) and document the intent
back to the user.

Unfortunately, as of writing many useful API around _slices of_
`MaybeUninit` are still unstable.
  • Loading branch information
MarijnS95 authored Jun 16, 2023
1 parent 54e5bfc commit a92cca4
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- **Breaking:** Upgrade `num_enum` crate from `0.5.1` to `0.6`. (#398)
- **Breaking:** Renamed and moved "`media`" error types and helpers to a new `media_error` module. (#399)
- **Breaking:** media_codec: Wrap common dequeued-buffer status codes in enum. (#401)
- **Breaking:** media_codec: Return `MaybeUninit` bytes in `buffer_mut()`. (#403)

# 0.7.0 (2022-07-24)

Expand Down
4 changes: 2 additions & 2 deletions ndk/src/media/media_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ pub struct InputBuffer<'a> {
}

impl InputBuffer<'_> {
pub fn buffer_mut(&mut self) -> &mut [u8] {
pub fn buffer_mut(&mut self) -> &mut [MaybeUninit<u8>] {
unsafe {
let mut out_size = 0;
let buffer_ptr =
Expand All @@ -476,7 +476,7 @@ impl InputBuffer<'_> {
"AMediaCodec_getInputBuffer returned NULL for index {}",
self.index
);
slice::from_raw_parts_mut(buffer_ptr, out_size)
slice::from_raw_parts_mut(buffer_ptr.cast(), out_size)
}
}
}
Expand Down

0 comments on commit a92cca4

Please sign in to comment.