Skip to content

Commit 4b0e174

Browse files
authored
Merge pull request #47 from Rust-for-Linux/fallible-alloc
Use fallible allocs in a few places
2 parents b39cf2c + 09c17f6 commit 4b0e174

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

rust/kernel/src/file_operations.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use alloc::boxed::Box;
88
use crate::bindings;
99
use crate::c_types;
1010
use crate::error::{Error, KernelResult};
11+
use crate::try_alloc;
1112
use crate::user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter};
1213

1314
bitflags::bitflags! {
@@ -65,7 +66,7 @@ unsafe extern "C" fn open_callback<T: FileOperations>(
6566
file: *mut bindings::file,
6667
) -> c_types::c_int {
6768
from_kernel_result! {
68-
let f = Box::new(T::open()?);
69+
let f = try_alloc(T::open()?)?;
6970
(*file).private_data = Box::into_raw(f) as *mut c_types::c_void;
7071
Ok(0)
7172
}
@@ -232,7 +233,7 @@ pub trait FileOperations: Sync + Sized {
232233
/// pointer in `struct file_operations`.
233234
const SEEK: SeekFn<Self> = None;
234235

235-
/// Syncs pending changes to this file. Corresponds to the `fsync` function
236+
/// Syncs pending changes to this file. Corresponds to the `fsync` function
236237
/// pointer in the `struct file_operations`.
237238
const FSYNC: FSync<Self> = None;
238239
}

rust/kernel/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub fn try_alloc<T>(value: T) -> KernelResult<Box<T>> {
6868
let layout = Layout::new::<MaybeUninit<T>>();
6969
let ptr: NonNull<MaybeUninit<T>> = if layout.size() == 0 {
7070
NonNull::dangling()
71-
// SAFETY: We checked that the layout size is nonzero.
71+
// SAFETY: We checked that the layout size is nonzero.
7272
} else if let Some(nn) = NonNull::new(unsafe { alloc::alloc::alloc(layout) }) {
7373
nn.cast()
7474
} else {

rust/kernel/src/sysctl.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use core::sync::atomic;
99
use crate::bindings;
1010
use crate::c_types;
1111
use crate::error;
12+
use crate::try_alloc;
1213
use crate::types;
1314
use crate::user_ptr::{UserSlicePtr, UserSlicePtrWriter};
1415

@@ -129,7 +130,7 @@ impl<T: SysctlStorage> Sysctl<T> {
129130
return Err(error::Error::EINVAL);
130131
}
131132

132-
let storage = Box::new(storage);
133+
let storage = try_alloc(storage)?;
133134
let mut table = vec![
134135
bindings::ctl_table {
135136
procname: name.as_ptr() as *const i8,

0 commit comments

Comments
 (0)