Skip to content

Commit 83b3b3a

Browse files
authored
ndk/hardware_buffer: Don't call assume_init() before checking error (#426)
We were unconditionally passing an `assume_init()` value to `status_to_io_result()`, which would only return that value back if there was no error. This is UB if the `MaybeUninit` was never written to, which is typically the case when an error is returned. Instead a `.map(|()| ...assume_init())` should be used to ensure we only move the `MaybeUninit` into an initialized Rust value when the error code says it is okay to do so. As this is the only place where a non-void (`()`) value was passed to `status_to_io_result()`, the `value: T` argument has been removed in favour of always returning `()` just like the `BitmapError::from_status()` and `MediaError::from_status()` APIs.
1 parent d64c8b6 commit 83b3b3a

File tree

5 files changed

+13
-13
lines changed

5 files changed

+13
-13
lines changed

ndk/src/hardware_buffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub type Rect = ffi::ARect;
108108
fn construct<T>(with_ptr: impl FnOnce(*mut T) -> i32) -> Result<T> {
109109
let mut result = MaybeUninit::uninit();
110110
let status = with_ptr(result.as_mut_ptr());
111-
status_to_io_result(status, unsafe { result.assume_init() })
111+
status_to_io_result(status).map(|()| unsafe { result.assume_init() })
112112
}
113113

114114
/// A native [`AHardwareBuffer *`]
@@ -317,7 +317,7 @@ impl HardwareBuffer {
317317
bytes_per_stride.as_mut_ptr(),
318318
)
319319
};
320-
status_to_io_result(status, ()).map(|()| unsafe {
320+
status_to_io_result(status).map(|()| unsafe {
321321
LockedPlaneInfo {
322322
virtual_address: virtual_address.assume_init(),
323323
bytes_per_pixel: bytes_per_pixel.assume_init() as u32,
@@ -372,7 +372,7 @@ impl HardwareBuffer {
372372
/// a non-blocking variant that returns a file descriptor to be signaled on unlocking instead.
373373
pub fn unlock(&self) -> Result<()> {
374374
let status = unsafe { ffi::AHardwareBuffer_unlock(self.as_ptr(), std::ptr::null_mut()) };
375-
status_to_io_result(status, ())
375+
status_to_io_result(status)
376376
}
377377

378378
/// Unlock the [`HardwareBuffer`] from direct CPU access.
@@ -413,7 +413,7 @@ impl HardwareBuffer {
413413
let status = unsafe {
414414
ffi::AHardwareBuffer_sendHandleToUnixSocket(self.as_ptr(), socket_fd.as_raw_fd())
415415
};
416-
status_to_io_result(status, ())
416+
status_to_io_result(status)
417417
}
418418

419419
/// Acquire a reference on the given [`HardwareBuffer`] object.

ndk/src/input_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl InputQueue {
4545
pub fn get_event(&self) -> Result<Option<InputEvent>> {
4646
let mut out_event = ptr::null_mut();
4747
let status = unsafe { ffi::AInputQueue_getEvent(self.ptr.as_ptr(), &mut out_event) };
48-
match status_to_io_result(status, ()) {
48+
match status_to_io_result(status) {
4949
Ok(()) => {
5050
debug_assert!(!out_event.is_null());
5151
Ok(Some(unsafe {

ndk/src/native_window.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl NativeWindow {
103103
let status = unsafe {
104104
ffi::ANativeWindow_setBuffersGeometry(self.ptr.as_ptr(), width, height, format as i32)
105105
};
106-
status_to_io_result(status, ())
106+
status_to_io_result(status)
107107
}
108108

109109
/// Return the [`NativeWindow`] associated with a JNI [`android.view.Surface`] pointer.
@@ -139,10 +139,10 @@ impl NativeWindow {
139139
None => std::ptr::null_mut(),
140140
};
141141
let mut buffer = MaybeUninit::uninit();
142-
let ret = unsafe {
142+
let status = unsafe {
143143
ffi::ANativeWindow_lock(self.ptr.as_ptr(), buffer.as_mut_ptr(), dirty_bounds)
144144
};
145-
status_to_io_result(ret, ())?;
145+
status_to_io_result(status)?;
146146

147147
Ok(NativeWindowBufferLockGuard {
148148
window: self,

ndk/src/surface_texture.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl SurfaceTexture {
8484
/// context at a time.
8585
pub fn attach_to_gl_context(&self, tex_name: u32) -> Result<()> {
8686
let status = unsafe { ffi::ASurfaceTexture_attachToGLContext(self.ptr.as_ptr(), tex_name) };
87-
status_to_io_result(status, ())
87+
status_to_io_result(status)
8888
}
8989

9090
/// Detach the [`SurfaceTexture`] from the OpenGL ES context that owns the OpenGL ES texture
@@ -100,7 +100,7 @@ impl SurfaceTexture {
100100
/// context at a time.
101101
pub fn detach_from_gl_context(&self) -> Result<()> {
102102
let status = unsafe { ffi::ASurfaceTexture_detachFromGLContext(self.ptr.as_ptr()) };
103-
status_to_io_result(status, ())
103+
status_to_io_result(status)
104104
}
105105

106106
/// Retrieve the 4x4 texture coordinate transform matrix associated with the texture image set
@@ -154,6 +154,6 @@ impl SurfaceTexture {
154154
/// texture target.
155155
pub fn update_tex_image(&self) -> Result<()> {
156156
let status = unsafe { ffi::ASurfaceTexture_updateTexImage(self.ptr.as_ptr()) };
157-
status_to_io_result(status, ())
157+
status_to_io_result(status)
158158
}
159159
}

ndk/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::io::{Error, Result};
77
/// Rust's [`std::io::Error`].
88
///
99
/// [`Errors.h`]: https://cs.android.com/android/platform/superproject/+/master:system/core/libutils/include/utils/Errors.h
10-
pub(crate) fn status_to_io_result<T>(status: i32, value: T) -> Result<T> {
10+
pub(crate) fn status_to_io_result(status: i32) -> Result<()> {
1111
match status {
12-
0 => Ok(value),
12+
0 => Ok(()),
1313
r if r < 0 => Err(Error::from_raw_os_error(-r)),
1414
r => unreachable!("Status is positive integer {}", r),
1515
}

0 commit comments

Comments
 (0)