From ed512c6867d212a54afa38038d6006362a7abaa5 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Sat, 7 May 2022 18:53:02 +0800 Subject: [PATCH] Fix issues detected by Coverity (#1154) wasm_c_api.c: add more checks, fix LOG_WARNING invalid specifier aot_emit_aot_file: fix strncpy max size length to copy posix.c: fix potential socket not close issue wasm-c-api samples: add return value checks for fseek/ftell cJSON.c: remove dead code --- core/iwasm/common/wasm_c_api.c | 15 ++- core/iwasm/compilation/aot_emit_aot_file.c | 2 +- core/iwasm/libraries/debug-engine/handler.c | 2 +- .../sandboxed-system-primitives/src/posix.c | 45 +++++---- samples/wasm-c-api/src/callback.c | 25 ++++- samples/wasm-c-api/src/callback_chain.c | 99 +++++++++++-------- samples/wasm-c-api/src/empty_imports.c | 25 ++++- samples/wasm-c-api/src/global.c | 25 ++++- samples/wasm-c-api/src/hello.c | 25 ++++- samples/wasm-c-api/src/hostref.c | 25 ++++- samples/wasm-c-api/src/memory.c | 25 ++++- samples/wasm-c-api/src/reflect.c | 25 ++++- samples/wasm-c-api/src/table.c | 25 ++++- samples/wasm-c-api/src/trap.c | 25 ++++- test-tools/host-tool/external/cJSON/cJSON.c | 4 - 15 files changed, 294 insertions(+), 98 deletions(-) diff --git a/core/iwasm/common/wasm_c_api.c b/core/iwasm/common/wasm_c_api.c index e5f1af302..7c3f5a83a 100644 --- a/core/iwasm/common/wasm_c_api.c +++ b/core/iwasm/common/wasm_c_api.c @@ -142,6 +142,9 @@ failed: \ void wasm_##name##_vec_copy(wasm_##name##_vec_t *out, \ const wasm_##name##_vec_t *src) \ { \ + if (!src) { \ + return; \ + } \ wasm_##name##_vec_new(out, src->size, src->data); \ } \ void wasm_##name##_vec_delete(wasm_##name##_vec_t *v) \ @@ -218,7 +221,7 @@ failed: \ if (!v) { \ return; \ } \ - for (i = 0; i != v->num_elems; ++i) { \ + for (i = 0; i != v->num_elems && v->data; ++i) { \ elem_destroy_func(*(v->data + i)); \ } \ bh_vector_destroy((Vector *)v); \ @@ -385,7 +388,8 @@ wasm_store_new(wasm_engine_t *engine) DEFAULT_VECTOR_INIT_LENGTH); if (!(store->foreigns = malloc_internal(sizeof(Vector))) - || !(bh_vector_init(store->foreigns, 24, sizeof(Vector *), true))) { + || !(bh_vector_init(store->foreigns, 24, sizeof(wasm_foreign_t *), + true))) { goto failed; } @@ -995,7 +999,7 @@ wasm_externtype_copy(const wasm_externtype_t *src) COPY_EXTERNTYPE(TABLE, tabletype) #undef COPY_EXTERNTYPE default: - LOG_WARNING("%s meets unsupported kind", __FUNCTION__, + LOG_WARNING("%s meets unsupported kind %u", __FUNCTION__, src->extern_kind); break; } @@ -1023,7 +1027,8 @@ wasm_externtype_delete(wasm_externtype_t *extern_type) wasm_tabletype_delete(wasm_externtype_as_tabletype(extern_type)); break; default: - LOG_WARNING("%s meets unsupported type", __FUNCTION__, extern_type); + LOG_WARNING("%s meets unsupported type %u", __FUNCTION__, + wasm_externtype_kind(extern_type)); break; } } @@ -2307,7 +2312,7 @@ wasm_module_exports(const wasm_module_t *module, wasm_exporttype_vec_t *out) } default: { - LOG_WARNING("%s meets unsupported type", __FUNCTION__, + LOG_WARNING("%s meets unsupported type %u", __FUNCTION__, export->kind); break; } diff --git a/core/iwasm/compilation/aot_emit_aot_file.c b/core/iwasm/compilation/aot_emit_aot_file.c index f8e30f564..f81a724b0 100644 --- a/core/iwasm/compilation/aot_emit_aot_file.c +++ b/core/iwasm/compilation/aot_emit_aot_file.c @@ -2104,7 +2104,7 @@ aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data) } strncpy(obj_data->target_info.arch, comp_ctx->target_arch, - sizeof(obj_data->target_info.arch)); + sizeof(obj_data->target_info.arch) - 1); return true; } diff --git a/core/iwasm/libraries/debug-engine/handler.c b/core/iwasm/libraries/debug-engine/handler.c index d39bb35b9..1b9bd8de4 100644 --- a/core/iwasm/libraries/debug-engine/handler.c +++ b/core/iwasm/libraries/debug-engine/handler.c @@ -471,7 +471,7 @@ handle_threadstop_request(WASMGDBServer *server, char *payload) void handle_set_current_thread(WASMGDBServer *server, char *payload) { - LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload, payload); + LOG_VERBOSE("%s:%s\n", __FUNCTION__, payload); if ('g' == *payload++) { uint64 tid = strtoll(payload, NULL, 16); if (tid > 0) diff --git a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c index 1e201aa27..142fca8a8 100644 --- a/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c +++ b/core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c @@ -656,12 +656,13 @@ fd_table_insert_fd(struct fd_table *ft, int in, __wasi_filetype_t type, REQUIRES_UNLOCKED(ft->lock) { struct fd_object *fo; - __wasi_errno_t error = fd_object_new(type, &fo); + __wasi_errno_t error = fd_object_new(type, &fo); if (error != 0) { close(in); return error; } + fo->number = in; if (type == __WASI_FILETYPE_DIRECTORY) { if (!mutex_init(&fo->directory.lock)) { @@ -2808,33 +2809,42 @@ wasi_ssp_sock_accept( __wasi_filetype_t wasi_type; __wasi_rights_t max_base, max_inheriting; struct fd_object *fo; - bh_socket_t new_sock; + bh_socket_t new_sock = -1; int ret; __wasi_errno_t error = fd_object_get(curfds, &fo, fd, __WASI_RIGHT_SOCK_ACCEPT, 0); - if (error != __WASI_ESUCCESS) - return error; + if (error != __WASI_ESUCCESS) { + goto fail; + } ret = os_socket_accept(fd_number(fo), &new_sock, NULL, NULL); fd_object_release(fo); - if (ret == BHT_ERROR) - return convert_errno(errno); + if (BHT_OK != ret) { + error = convert_errno(errno); + goto fail; + } error = fd_determine_type_rights(new_sock, &wasi_type, &max_base, &max_inheriting); if (error != __WASI_ESUCCESS) { - os_socket_close(ret); - return error; + goto fail; } error = fd_table_insert_fd(curfds, new_sock, wasi_type, max_base, max_inheriting, fd_new); if (error != __WASI_ESUCCESS) { - os_socket_close(ret); - return error; + /* released in fd_table_insert_fd() */ + new_sock = -1; + goto fail; } return __WASI_ESUCCESS; + +fail: + if (-1 != new_sock) { + os_socket_close(new_sock); + } + return error; } __wasi_errno_t @@ -2898,7 +2908,7 @@ wasi_ssp_sock_bind( ret = os_socket_bind(fd_number(fo), buf, &port); fd_object_release(fo); - if (ret == BHT_ERROR) { + if (BHT_OK != ret) { return convert_errno(errno); } @@ -2931,7 +2941,7 @@ wasi_ssp_sock_connect( ret = os_socket_connect(fd_number(fo), buf, addr->addr.ip4.port); fd_object_release(fo); - if (ret == BHT_ERROR) { + if (BHT_OK != ret) { return convert_errno(errno); } @@ -2954,7 +2964,7 @@ wasi_ssp_sock_listen( ret = os_socket_listen(fd_number(fo), backlog); fd_object_release(fo); - if (ret == BHT_ERROR) { + if (BHT_OK != ret) { return convert_errno(errno); } @@ -2985,7 +2995,7 @@ wasi_ssp_sock_open( tcp_or_udp = SOCKET_DGRAM == socktype ? 0 : 1; ret = os_socket_create(&sock, tcp_or_udp); - if (ret == BHT_ERROR) { + if (BHT_OK != ret) { return convert_errno(errno); } @@ -3007,7 +3017,6 @@ wasi_ssp_sock_open( error = fd_table_insert_fd(curfds, sock, wasi_type, max_base, max_inheriting, sockfd); if (error != __WASI_ESUCCESS) { - os_socket_close(sock); return error; } @@ -3032,7 +3041,7 @@ wasmtime_ssp_sock_recv( ret = os_socket_recv(fd_number(fo), buf, buf_len); fd_object_release(fo); - if (ret == BHT_ERROR) { + if (BHT_OK != ret) { return convert_errno(errno); } @@ -3058,7 +3067,7 @@ wasmtime_ssp_sock_send( ret = os_socket_send(fd_number(fo), buf, buf_len); fd_object_release(fo); - if (ret == BHT_ERROR) { + if (BHT_OK != ret) { return convert_errno(errno); } @@ -3083,7 +3092,7 @@ wasmtime_ssp_sock_shutdown( ret = os_socket_shutdown(fd_number(fo)); fd_object_release(fo); - if (ret == BHT_ERROR) + if (BHT_OK != ret) return convert_errno(errno); return __WASI_ESUCCESS; diff --git a/samples/wasm-c-api/src/callback.c b/samples/wasm-c-api/src/callback.c index fc6c109db..b1294670d 100644 --- a/samples/wasm-c-api/src/callback.c +++ b/samples/wasm-c-api/src/callback.c @@ -77,9 +77,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/callback_chain.c b/samples/wasm-c-api/src/callback_chain.c index 64f42d960..b1f3eb108 100644 --- a/samples/wasm-c-api/src/callback_chain.c +++ b/samples/wasm-c-api/src/callback_chain.c @@ -16,46 +16,45 @@ static const byte_t * get_memory_data(uint32_t offset, uint32_t length); static bool -call_wasm_function(uint32_t export_id, - const wasm_val_vec_t *args, - wasm_val_vec_t *results, - const char *name); +call_wasm_function(uint32_t export_id, const wasm_val_vec_t *args, + wasm_val_vec_t *results, const char *name); /************************ IMPORTED FUNCTIONS **************************/ // (nil) -> i32 #define FUNCTION_TYPE_NIL_I32 wasm_functype_new_0_1(wasm_valtype_new_i32()) // (i32, i32) -> nil -#define FUNCTION_TYPE_I32X2_NIL \ +#define FUNCTION_TYPE_I32X2_NIL \ wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32()) /* IMPORT FUNCTION LIST */ -#define IMPORT_FUNCTION_LIST(V) \ - V(get_pairs, 0, FUNCTION_TYPE_NIL_I32) \ +#define IMPORT_FUNCTION_LIST(V) \ + V(get_pairs, 0, FUNCTION_TYPE_NIL_I32) \ V(log, 1, FUNCTION_TYPE_I32X2_NIL) /* EXPORT FUNCTION LIST */ -#define EXPORT_FUNCTION_LIST(V) \ - V(on_start) \ - V(on_stop) \ - V(malloc) \ +#define EXPORT_FUNCTION_LIST(V) \ + V(on_start) \ + V(on_stop) \ + V(malloc) \ V(free) enum EXPORT_ITEM_NAME { #define DEFINE_ENUM(name) e_##name, EXPORT_FUNCTION_LIST(DEFINE_ENUM) #undef DEFINE_ENUM - e_MEMORY, + e_MEMORY, }; -#define DEFINE_FUNCTION(name) \ - wasm_trap_t *STUB_##name(const wasm_val_vec_t* args, wasm_val_vec_t* results) +#define DEFINE_FUNCTION(name) \ + wasm_trap_t *STUB_##name(const wasm_val_vec_t *args, \ + wasm_val_vec_t *results) -#define DEFINE_EMPTY_FUNCTION(name) \ - DEFINE_FUNCTION(name) \ - { \ - printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__); \ - return NULL; \ +#define DEFINE_EMPTY_FUNCTION(name) \ + DEFINE_FUNCTION(name) \ + { \ + printf("[WASM -> NATIVE] calling back %s\n", __FUNCTION__); \ + return NULL; \ } #undef DEFINE_EMPTY_FUNCTION @@ -91,9 +90,8 @@ DEFINE_FUNCTION(log) } /**********************************************************************/ -// all exportted wasm functions. check with "/opt/wabt/bin/wasm-objdump -x -j Export X.wasm" -// -1: memory -// 0-32: functions +// all exportted wasm functions. check with "/opt/wabt/bin/wasm-objdump -x -j +// Export X.wasm" -1: memory 0-32: functions static own wasm_extern_vec_t exports = { 0 }; static const byte_t * @@ -117,10 +115,8 @@ get_memory_data(uint32_t offset, uint32_t length) } static bool -call_wasm_function(uint32_t export_id, - const wasm_val_vec_t *args, - wasm_val_vec_t *results, - const char *name) +call_wasm_function(uint32_t export_id, const wasm_val_vec_t *args, + wasm_val_vec_t *results, const char *name) { const wasm_func_t *function; wasm_trap_t *trap; @@ -169,9 +165,28 @@ main(int argc, const char *argv[]) printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { @@ -196,38 +211,38 @@ main(int argc, const char *argv[]) // Create external functions. printf("Creating callback...\n"); -#define IMPORT_FUNCTION_VARIABLE_NAME(name, ...) \ +#define IMPORT_FUNCTION_VARIABLE_NAME(name, ...) \ own wasm_func_t *function_##name = NULL; IMPORT_FUNCTION_LIST(IMPORT_FUNCTION_VARIABLE_NAME) #undef IMPORT_FUNCTION_VARIABLE_NAME -#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE) \ - { \ - own wasm_functype_t *type = CREATE_FUNC_TYPE; \ - if (!(function_##name = wasm_func_new(store, type, STUB_##name))) { \ - printf("> Error creating new function\n"); \ - return 1; \ - } \ - wasm_functype_delete(type); \ +#define CREATE_WASM_FUNCTION(name, index, CREATE_FUNC_TYPE) \ + { \ + own wasm_functype_t *type = CREATE_FUNC_TYPE; \ + if (!(function_##name = wasm_func_new(store, type, STUB_##name))) { \ + printf("> Error creating new function\n"); \ + return 1; \ + } \ + wasm_functype_delete(type); \ } IMPORT_FUNCTION_LIST(CREATE_WASM_FUNCTION) #undef CREATE_WASM_FUNCTION - wasm_extern_t *fs[10] = {0}; -#define ADD_TO_FUNCTION_LIST(name, index, ...) \ + wasm_extern_t *fs[10] = { 0 }; +#define ADD_TO_FUNCTION_LIST(name, index, ...) \ fs[index] = wasm_func_as_extern(function_##name); IMPORT_FUNCTION_LIST(ADD_TO_FUNCTION_LIST) #undef ADD_TO_FUNCTION_LIST wasm_extern_vec_t imports = WASM_ARRAY_VEC(fs); own wasm_instance_t *instance = - wasm_instance_new(store, module, &imports, NULL); + wasm_instance_new(store, module, &imports, NULL); if (!instance) { printf("> Error instantiating module!\n"); return 1; } -#define DESTROY_WASM_FUNCITON(name, index, ...) \ +#define DESTROY_WASM_FUNCITON(name, index, ...) \ wasm_func_delete(function_##name); IMPORT_FUNCTION_LIST(DESTROY_WASM_FUNCITON) #undef DESTROY_WASM_FUNCITON diff --git a/samples/wasm-c-api/src/empty_imports.c b/samples/wasm-c-api/src/empty_imports.c index 902704ed8..5858ed6b5 100644 --- a/samples/wasm-c-api/src/empty_imports.c +++ b/samples/wasm-c-api/src/empty_imports.c @@ -21,9 +21,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/global.c b/samples/wasm-c-api/src/global.c index f16af9cc9..bc4671d23 100644 --- a/samples/wasm-c-api/src/global.c +++ b/samples/wasm-c-api/src/global.c @@ -64,9 +64,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/hello.c b/samples/wasm-c-api/src/hello.c index 6b566447b..5a2b9843e 100644 --- a/samples/wasm-c-api/src/hello.c +++ b/samples/wasm-c-api/src/hello.c @@ -34,9 +34,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/hostref.c b/samples/wasm-c-api/src/hostref.c index 211f0cffd..a411ddd9f 100644 --- a/samples/wasm-c-api/src/hostref.c +++ b/samples/wasm-c-api/src/hostref.c @@ -139,9 +139,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/memory.c b/samples/wasm-c-api/src/memory.c index c09a410ee..64d1c0777 100644 --- a/samples/wasm-c-api/src/memory.c +++ b/samples/wasm-c-api/src/memory.c @@ -110,9 +110,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/reflect.c b/samples/wasm-c-api/src/reflect.c index 5967bad1e..a595fd3e0 100644 --- a/samples/wasm-c-api/src/reflect.c +++ b/samples/wasm-c-api/src/reflect.c @@ -111,9 +111,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/table.c b/samples/wasm-c-api/src/table.c index 739385e93..0bf68e05a 100644 --- a/samples/wasm-c-api/src/table.c +++ b/samples/wasm-c-api/src/table.c @@ -90,9 +90,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/samples/wasm-c-api/src/trap.c b/samples/wasm-c-api/src/trap.c index 5feb9511d..230b2d4e2 100644 --- a/samples/wasm-c-api/src/trap.c +++ b/samples/wasm-c-api/src/trap.c @@ -47,9 +47,28 @@ int main(int argc, const char* argv[]) { printf("> Error loading module!\n"); return 1; } - fseek(file, 0L, SEEK_END); - size_t file_size = ftell(file); - fseek(file, 0L, SEEK_SET); + + int ret = fseek(file, 0L, SEEK_END); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + long file_size = ftell(file); + if (file_size == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + + ret = fseek(file, 0L, SEEK_SET); + if (ret == -1) { + printf("> Error loading module!\n"); + fclose(file); + return 1; + } + wasm_byte_vec_t binary; wasm_byte_vec_new_uninitialized(&binary, file_size); if (fread(binary.data, file_size, 1, file) != 1) { diff --git a/test-tools/host-tool/external/cJSON/cJSON.c b/test-tools/host-tool/external/cJSON/cJSON.c index 7feb6a471..2e35351db 100644 --- a/test-tools/host-tool/external/cJSON/cJSON.c +++ b/test-tools/host-tool/external/cJSON/cJSON.c @@ -1113,10 +1113,6 @@ fail: hooks->deallocate(buffer->buffer); } - if (printed != NULL) { - hooks->deallocate(printed); - } - return NULL; }