Skip to content
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

feat(runtime-c-api) Check buffer size before creating the slice, and fix wasmer_last_error_message returned value #432

Merged
merged 6 commits into from May 13, 2019
16 changes: 8 additions & 8 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
Hywan marked this conversation as resolved.
Show resolved Hide resolved
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 All @@ -85,7 +85,7 @@ pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length:
// accidentally read into garbage.
buffer[error_message.len()] = 0;

error_message.len() as c_int
error_message.len() as c_int + 1
}

#[derive(Debug)]
Expand Down