mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-13 06:15:33 +00:00
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:
parent
7f61eafe05
commit
1d555301f7
@ -61,20 +61,20 @@ pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let last_error = match take_last_error() {
|
let error_message = match take_last_error() {
|
||||||
Some(err) => err,
|
Some(err) => err.to_string(),
|
||||||
None => return 0,
|
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() >= length {
|
||||||
|
// buffer to small to hold the error message
|
||||||
if error_message.len() >= buffer.len() {
|
|
||||||
// buffer to small for err message
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let buffer = slice::from_raw_parts_mut(buffer as *mut u8, length);
|
||||||
|
|
||||||
ptr::copy_nonoverlapping(
|
ptr::copy_nonoverlapping(
|
||||||
error_message.as_ptr(),
|
error_message.as_ptr(),
|
||||||
buffer.as_mut_ptr(),
|
buffer.as_mut_ptr(),
|
||||||
|
Loading…
Reference in New Issue
Block a user