Fix wasm loader malloc(0) issue which returns NULL is some platforms (#397)

Signed-off-by: Huang Qi <huangqi3@xiaomi.com>

Co-authored-by: Huang Qi <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi 2020-09-23 15:54:22 +08:00 committed by GitHub
parent d8d367b367
commit a290aaf93e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -1691,8 +1691,14 @@ init_function_local_offsets(WASMFunction *func,
uint32 i, local_offset = 0;
uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
if (!(func->local_offsets =
loader_malloc(total_size, error_buf, error_buf_size))) {
/*
* Only allocate memory when total_size is not 0,
* or the return value of malloc(0) might be NULL on some platforms,
* which causes wasm loader return false.
*/
if (total_size > 0
&& !(func->local_offsets =
loader_malloc(total_size, error_buf, error_buf_size))) {
return false;
}

View File

@ -853,8 +853,9 @@ init_function_local_offsets(WASMFunction *func,
uint32 i, local_offset = 0;
uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
if (!(func->local_offsets =
loader_malloc(total_size, error_buf, error_buf_size))) {
if (total_size > 0
&& !(func->local_offsets =
loader_malloc(total_size, error_buf, error_buf_size))) {
return false;
}