Skip to content

Commit

Permalink
Merge pull request torvalds#124 from wedsonaf/user_ptr-alloc
Browse files Browse the repository at this point in the history
Handle possible allocation failure in `user_ptr`.
  • Loading branch information
alex authored Mar 21, 2021
2 parents e9b9c0e + 6f6647a commit 72904b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
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
)]
#![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

0 comments on commit 72904b1

Please sign in to comment.