feat(runtime-c-api) Check buffer size before creating the slice.

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.
This commit is contained in:
Ivan Enderlin 2019-05-10 15:55:02 +02:00
parent 7f61eafe05
commit 1d555301f7

View File

@ -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(),