Skip to content

Use fallible allocs in a few places #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rust/kernel/src/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use alloc::boxed::Box;
use crate::bindings;
use crate::c_types;
use crate::error::{Error, KernelResult};
use crate::try_alloc;
use crate::user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter};

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

/// Syncs pending changes to this file. Corresponds to the `fsync` function
/// Syncs pending changes to this file. Corresponds to the `fsync` function
/// pointer in the `struct file_operations`.
const FSYNC: FSync<Self> = None;
}
2 changes: 1 addition & 1 deletion rust/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn try_alloc<T>(value: T) -> KernelResult<Box<T>> {
let layout = Layout::new::<MaybeUninit<T>>();
let ptr: NonNull<MaybeUninit<T>> = if layout.size() == 0 {
NonNull::dangling()
// SAFETY: We checked that the layout size is nonzero.
// SAFETY: We checked that the layout size is nonzero.
} else if let Some(nn) = NonNull::new(unsafe { alloc::alloc::alloc(layout) }) {
nn.cast()
} else {
Expand Down
3 changes: 2 additions & 1 deletion rust/kernel/src/sysctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use core::sync::atomic;
use crate::bindings;
use crate::c_types;
use crate::error;
use crate::try_alloc;
use crate::types;
use crate::user_ptr::{UserSlicePtr, UserSlicePtrWriter};

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

let storage = Box::new(storage);
let storage = try_alloc(storage)?;
let mut table = vec![
bindings::ctl_table {
procname: name.as_ptr() as *const i8,
Expand Down