From b1173d1bfc7454e528ba9fd5680221acd3a0511d Mon Sep 17 00:00:00 2001 From: Saju Pillai Date: Sun, 12 Sep 2021 18:55:05 -0700 Subject: [PATCH] Allocate correct space for results before native callback is run (#747) --- core/iwasm/common/wasm_runtime_common.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index a65a63d3..6910aacb 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -4117,6 +4117,13 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, goto fail; } + if (func_type->result_count > 4 + && !(results = wasm_runtime_malloc(sizeof(wasm_val_t) + * func_type->result_count))) { + wasm_runtime_set_exception(module_inst, "allocate memory failed"); + goto fail; + } + if (!with_env) { wasm_func_callback_t callback = (wasm_func_callback_t)func_ptr; trap = callback(params, results); @@ -4144,13 +4151,6 @@ wasm_runtime_invoke_c_api_native(WASMModuleInstanceCommon *module_inst, goto fail; } - if (func_type->result_count > 4 - && !(results = wasm_runtime_malloc(sizeof(wasm_val_t) - * func_type->result_count))) { - wasm_runtime_set_exception(module_inst, "allocate memory failed"); - goto fail; - } - if (!results_to_argv(module_inst, argv, results, func_type)) { wasm_runtime_set_exception(module_inst, "unsupported result type"); goto fail;