From 1d555301f7d86fb5ee75b17a588a92f4a851a5ca Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 10 May 2019 15:55:02 +0200 Subject: [PATCH 1/4] 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(), From 7d2721ef7a5d08c83487c43dc514e6e01ad04897 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 10 May 2019 16:00:13 +0200 Subject: [PATCH 2/4] fix(runtime-c-api) `wasmer_last_error_message` returns the length + 1. Returning the error message's length + 1 mimics the `wasmer_last_error_length` function that counts the trailing null byte. --- lib/runtime-c-api/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-c-api/src/error.rs b/lib/runtime-c-api/src/error.rs index fda6c77b3fe..0a578f9e20c 100644 --- a/lib/runtime-c-api/src/error.rs +++ b/lib/runtime-c-api/src/error.rs @@ -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)] From bedd305b06166262bf8f976f1464571de84b65b9 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 13 May 2019 10:52:43 +0200 Subject: [PATCH 3/4] doc(runtime-c-api) Fix inline comment. --- lib/runtime-c-api/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime-c-api/src/error.rs b/lib/runtime-c-api/src/error.rs index 0a578f9e20c..d5fcfe35499 100644 --- a/lib/runtime-c-api/src/error.rs +++ b/lib/runtime-c-api/src/error.rs @@ -69,7 +69,7 @@ pub unsafe extern "C" fn wasmer_last_error_message(buffer: *mut c_char, length: let length = length as usize; if error_message.len() >= length { - // buffer to small to hold the error message + // buffer is too small to hold the error message return -1; } From 8c8586faaa204e647c3bd4f40cadf1aa4d405c35 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 13 May 2019 10:52:55 +0200 Subject: [PATCH 4/4] test(runtime-c-api) Assert returned value from `wasmer_last_error_message`. --- lib/runtime-c-api/tests/test-instantiate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/runtime-c-api/tests/test-instantiate.c b/lib/runtime-c-api/tests/test-instantiate.c index 20332623ebb..8a7c2610edc 100644 --- a/lib/runtime-c-api/tests/test-instantiate.c +++ b/lib/runtime-c-api/tests/test-instantiate.c @@ -46,7 +46,8 @@ int main() int error_len = wasmer_last_error_length(); printf("Error len: `%d`\n", error_len); char *error_str = malloc(error_len); - wasmer_last_error_message(error_str, error_len); + int error_result = wasmer_last_error_message(error_str, error_len); + assert(error_len == error_result); printf("Error str: `%s`\n", error_str); assert(0 == strcmp(error_str, "Call error: Parameters of type [I32] did not match signature [I32, I32] -> [I32]")); free(error_str);