Skip to content

Handle possible allocation failure in user_ptr. #124

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
Mar 21, 2021
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
16 changes: 9 additions & 7 deletions rust/kernel/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
//!
//! C header: [`include/uapi/asm-generic/errno-base.h`](../../../include/uapi/asm-generic/errno-base.h)

use core::num::TryFromIntError;
use core::str::Utf8Error;

use alloc::alloc::AllocError;

use crate::bindings;
use crate::c_types;
use crate::{bindings, c_types};
use alloc::{alloc::AllocError, collections::TryReserveError};
use core::{num::TryFromIntError, str::Utf8Error};

/// Generic integer kernel error.
///
Expand Down Expand Up @@ -72,6 +68,12 @@ impl From<Utf8Error> for Error {
}
}

impl From<TryReserveError> for Error {
fn from(_: TryReserveError) -> Error {
Error::ENOMEM
}
}

/// A [`Result`] with an [`Error`] error type.
///
/// To be used as the return type for functions that may fail.
Expand Down
8 changes: 7 additions & 1 deletion rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
//! do so first instead of bypassing this crate.

#![no_std]
#![feature(allocator_api, alloc_error_handler, const_fn, const_mut_refs)]
#![feature(
allocator_api,
alloc_error_handler,
const_fn,
const_mut_refs,
try_reserve
)]
Comment on lines +15 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, one per line is best for Git logs later. Even better, putting the full #[feature(...)] for each of them helps grepping for them + one less change when we remove all of them.

#![deny(clippy::complexity)]
#![deny(clippy::correctness)]
#![deny(clippy::perf)]
Expand Down
10 changes: 4 additions & 6 deletions rust/kernel/user_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@
//!
//! C header: [`include/linux/uaccess.h`](../../../../include/linux/uaccess.h)

use alloc::vec;
use crate::{c_types, error};
use alloc::vec::Vec;
use core::u32;

use crate::c_types;
use crate::error;

extern "C" {
fn rust_helper_access_ok(addr: *const c_types::c_void, len: c_types::c_ulong)
Expand Down Expand Up @@ -134,7 +130,9 @@ impl UserSlicePtrReader {
/// Returns `EFAULT` if the address does not currently point to
/// mapped, readable memory.
pub fn read_all(&mut self) -> error::KernelResult<Vec<u8>> {
let mut data = vec![0; self.1];
let mut data = Vec::<u8>::new();
data.try_reserve_exact(self.1)?;
data.resize(self.1, 0);
self.read(&mut data)?;
Ok(data)
}
Expand Down