From 474f081f56655dc44941b01370e8b346e1eebe52 Mon Sep 17 00:00:00 2001 From: Xu Jun <693788454@qq.com> Date: Sat, 7 May 2022 19:22:00 +0800 Subject: [PATCH] Fix return value not checked issue reported by Coverity (#1156) Fix return value not checked issue in function init_wasm_timer, reported by Coverity --- core/app-framework/base/native/runtime_lib.h | 2 +- .../app-framework/base/native/timer_wrapper.c | 27 +++++++++++++++---- .../src/platform/linux/iwasm_main.c | 8 ++++-- .../src/platform/zephyr/iwasm_main.c | 8 +++--- .../src/platform/linux/iwasm_main.c | 8 ++++-- .../src/platform/zephyr/iwasm_main.c | 8 +++--- samples/simple/src/iwasm_main.c | 10 ++++--- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/core/app-framework/base/native/runtime_lib.h b/core/app-framework/base/native/runtime_lib.h index ec4d84cb7..477b663b2 100644 --- a/core/app-framework/base/native/runtime_lib.h +++ b/core/app-framework/base/native/runtime_lib.h @@ -8,7 +8,7 @@ #include "runtime_timer.h" -void +bool init_wasm_timer(); void exit_wasm_timer(); diff --git a/core/app-framework/base/native/timer_wrapper.c b/core/app-framework/base/native/timer_wrapper.c index 7ad350cf2..246868849 100644 --- a/core/app-framework/base/native/timer_wrapper.c +++ b/core/app-framework/base/native/timer_wrapper.c @@ -83,19 +83,36 @@ wakeup_modules_timer_thread(timer_ctx_t ctx) os_mutex_unlock(&g_timer_ctx_list_mutex); } -void +bool init_wasm_timer() { korp_tid tm_tid; bh_list_init(&g_timer_ctx_list); - os_cond_init(&g_timer_ctx_list_cond); + if (os_cond_init(&g_timer_ctx_list_cond) != 0) { + return false; + } /* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */ - os_recursive_mutex_init(&g_timer_ctx_list_mutex); + if (os_recursive_mutex_init(&g_timer_ctx_list_mutex) != 0) { + goto fail1; + } - os_thread_create(&tm_tid, thread_modulers_timer_check, NULL, - BH_APPLET_PRESERVED_STACK_SIZE); + if (0 + != os_thread_create(&tm_tid, thread_modulers_timer_check, NULL, + BH_APPLET_PRESERVED_STACK_SIZE)) { + goto fail2; + } + + return true; + +fail2: + os_mutex_destroy(&g_timer_ctx_list_mutex); + +fail1: + os_cond_destroy(&g_timer_ctx_list_cond); + + return false; } void diff --git a/samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c b/samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c index 418d0ca26..61c7bb39d 100644 --- a/samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c +++ b/samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c @@ -531,8 +531,10 @@ iwasm_main(int argc, char *argv[]) goto fail2; } - // timer manager - init_wasm_timer(); + /* timer manager */ + if (!init_wasm_timer()) { + goto fail3; + } #ifndef CONNECTION_UART if (server_mode) @@ -548,6 +550,8 @@ iwasm_main(int argc, char *argv[]) app_manager_startup(&interface); exit_wasm_timer(); + +fail3: exit_sensor_framework(); fail2: diff --git a/samples/gui/wasm-runtime-wgl/src/platform/zephyr/iwasm_main.c b/samples/gui/wasm-runtime-wgl/src/platform/zephyr/iwasm_main.c index 0f683addf..6a88f8007 100644 --- a/samples/gui/wasm-runtime-wgl/src/platform/zephyr/iwasm_main.c +++ b/samples/gui/wasm-runtime-wgl/src/platform/zephyr/iwasm_main.c @@ -177,12 +177,14 @@ iwasm_main() wgl_init(); hal_init(); - // timer manager - init_wasm_timer(); + /* timer manager */ + if (!init_wasm_timer()) { + goto fail; + } - // TODO: app_manager_startup(&interface); +fail: wasm_runtime_destroy(); return -1; } diff --git a/samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c b/samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c index 9fb63c832..e2253bb5e 100644 --- a/samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c +++ b/samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c @@ -511,8 +511,10 @@ iwasm_main(int argc, char *argv[]) goto fail2; } - // timer manager - init_wasm_timer(); + /* timer manager */ + if (!init_wasm_timer()) { + goto fail3; + } #ifndef CONNECTION_UART if (server_mode) @@ -528,6 +530,8 @@ iwasm_main(int argc, char *argv[]) app_manager_startup(&interface); exit_wasm_timer(); + +fail3: exit_sensor_framework(); fail2: diff --git a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c index 9cb268847..dceb5786b 100644 --- a/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c +++ b/samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c @@ -118,12 +118,14 @@ iwasm_main() display_init(); - // timer manager - init_wasm_timer(); + /* timer manager */ + if (!init_wasm_timer()) { + goto fail; + } - // TODO: app_manager_startup(&interface); +fail: wasm_runtime_destroy(); return -1; } diff --git a/samples/simple/src/iwasm_main.c b/samples/simple/src/iwasm_main.c index d6315f3a9..36fb35b12 100644 --- a/samples/simple/src/iwasm_main.c +++ b/samples/simple/src/iwasm_main.c @@ -518,9 +518,6 @@ iwasm_main(int argc, char *argv[]) return -1; } - /* timer manager */ - init_wasm_timer(); - /* connection framework */ if (!init_connection_framework()) { goto fail1; @@ -531,6 +528,11 @@ iwasm_main(int argc, char *argv[]) goto fail2; } + /* timer manager */ + if (!init_wasm_timer()) { + goto fail3; + } + /* add the sys sensor objects */ add_sys_sensor("sensor_test1", "This is a sensor for test", 0, 1000, read_test_sensor, config_test_sensor); @@ -552,6 +554,8 @@ iwasm_main(int argc, char *argv[]) app_manager_startup(&interface); exit_wasm_timer(); + +fail3: exit_sensor_framework(); fail2: