Skip to content

Commit

Permalink
feat(runtime-c-api) Check buffer size before creating the slice.
Browse files Browse the repository at this point in the history
It's safer to check the buffer size is large enough to hold the error
message before creating the slice from raw parts.

Also, this patch remove the need for `last_error`, simplifying the
code a little bit. The `length` variable is casted to `usize` once.
  • Loading branch information
Hywan committed May 10, 2019
1 parent 7f61eaf commit 1d55530
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/runtime-c-api/src/error.rs
Expand Up @@ -61,20 +61,20 @@ pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length:
return -1;
}

let last_error = match take_last_error() {
Some(err) => err,
let error_message = match take_last_error() {
Some(err) => err.to_string(),
None => return 0,
};

let error_message = last_error.to_string();
let length = length as usize;

let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length as usize);

if error_message.len() >= buffer.len() {
// buffer to small for err message
if error_message.len() >= length {
// buffer to small to hold the error message
return -1;
}

let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length);

ptr::copy_nonoverlapping(
error_message.as_ptr(),
buffer.as_mut_ptr(),
Expand Down

0 comments on commit 1d55530

Please sign in to comment.