From 1d555301f7d86fb5ee75b17a588a92f4a851a5ca Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 10 May 2019 15:55:02 +0200 Subject: [PATCH] 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. --- lib/runtime-c-api/src/error.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/runtime-c-api/src/error.rs b/lib/runtime-c-api/src/error.rs index 5980c79a9eb..fda6c77b3fe 100644 --- a/lib/runtime-c-api/src/error.rs +++ b/lib/runtime-c-api/src/error.rs @@ -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(),