Skip to content

Commit a7f4c3c

Browse files
Fix app manager/framework issues reported by Coverity (#1155)
runtime_sensor.c: add return value check for os_mutex_init fix find_sensor_client sensor_mgr_ref.c: add return value check for init_sensor_framework app_manager_host.c: add return value check for app_manager_host_init module_wasm_app.c: add bh_assert for m_data fix mkdir potential issue sample littlevgl/gui/simple: add return value check for init_sensor_framework host_tool: add more check for g_conn_fd
1 parent 3edb832 commit a7f4c3c

11 files changed

Lines changed: 66 additions & 30 deletions

File tree

core/app-framework/sensor/native/runtime_sensor.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,13 @@ add_sys_sensor(char *name, char *description, int instance,
309309
g_sys_sensors = s;
310310
}
311311

312-
os_mutex_init(&s->lock);
312+
if (os_mutex_init(&s->lock) != 0) {
313+
if (s->description) {
314+
wasm_runtime_free(s->description);
315+
}
316+
wasm_runtime_free(s->name);
317+
wasm_runtime_free(s);
318+
}
313319

314320
return s;
315321
}
@@ -358,6 +364,7 @@ find_sensor_client(sys_sensor_t *sensor, unsigned int client_id,
358364
return c;
359365
}
360366
else {
367+
prev = c;
361368
c = c->next;
362369
}
363370
}

core/app-framework/sensor/native/runtime_sensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ check_sensor_timers();
5959
void
6060
reschedule_sensor_read();
6161

62-
void
62+
bool
6363
init_sensor_framework();
6464
void
6565
start_sensor_framework();

core/app-framework/sensor/native/sensor_mgr_ref.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,27 @@ cb_wakeup_thread()
106106
void
107107
set_sensor_reshceduler(void (*callback)());
108108

109-
void
109+
bool
110110
init_sensor_framework()
111111
{
112-
// init the mutext and conditions
113-
os_cond_init(&cond);
114-
os_mutex_init(&mutex);
112+
/* init the mutext and conditions */
113+
if (os_cond_init(&cond) != 0) {
114+
return false;
115+
}
116+
117+
if (os_mutex_init(&mutex) != 0) {
118+
os_cond_destroy(&cond);
119+
return false;
120+
}
115121

116122
set_sensor_reshceduler(cb_wakeup_thread);
117123

118124
wasm_register_msg_callback(SENSOR_EVENT_WASM,
119125
app_mgr_sensor_event_callback);
120126

121127
wasm_register_cleanup_callback(sensor_cleanup_callback);
128+
129+
return true;
122130
}
123131

124132
void

core/app-mgr/app-manager/app_manager_host.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,15 +264,21 @@ aee_host_msg_callback(void *msg, uint32_t msg_len)
264264
bool
265265
app_manager_host_init(host_interface *interface)
266266
{
267-
os_mutex_init(&host_lock);
267+
if (os_mutex_init(&host_lock) != 0) {
268+
return false;
269+
}
268270
memset(&recv_ctx, 0, sizeof(recv_ctx));
269271

270272
host_commu.init = interface->init;
271273
host_commu.send = interface->send;
272274
host_commu.destroy = interface->destroy;
273275

274-
if (host_commu.init != NULL)
275-
return host_commu.init();
276+
if (host_commu.init != NULL) {
277+
if (!host_commu.init()) {
278+
os_mutex_destroy(&host_lock);
279+
return false;
280+
}
281+
}
276282

277283
return true;
278284
}

core/app-mgr/app-manager/module_wasm_app.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ app_instance_queue_callback(void *queue_msg, void *arg)
207207

208208
wasm_module_inst_t inst = (wasm_module_inst_t)arg;
209209
module_data *m_data = app_manager_get_module_data(Module_WASM_App, inst);
210-
wasm_data *wasm_app_data = (wasm_data *)m_data->internal_data;
211-
int message_type = bh_message_type(queue_msg);
210+
wasm_data *wasm_app_data;
211+
int message_type;
212212

213213
bh_assert(m_data);
214+
wasm_app_data = (wasm_data *)m_data->internal_data;
215+
message_type = bh_message_type(queue_msg);
214216

215217
if (message_type < BASE_EVENT_MAX) {
216218
switch (message_type) {
@@ -410,16 +412,15 @@ wasm_app_prepare_wasi_dir(wasm_module_t module, const char *module_name,
410412
p += module_name_len;
411413
*p++ = '\0';
412414

413-
/* Create a wasi dir for the module */
414-
if (stat(wasi_dir_buf, &st) == 0) {
415-
/* exist, but is a regular file, not a dir */
416-
if (st.st_mode & S_IFREG)
417-
return false;
418-
}
419-
else {
420-
/* not exist, create it */
421-
if (mkdir(wasi_dir_buf, 0777) != 0)
422-
return false;
415+
if (mkdir(wasi_dir_buf, 0777) != 0) {
416+
if (errno == EEXIST) {
417+
/* Failed due to dir already exist */
418+
if ((stat(wasi_dir_buf, &st) == 0) && (st.st_mode & S_IFDIR)) {
419+
return true;
420+
}
421+
}
422+
423+
return false;
423424
}
424425

425426
return true;

samples/gui/wasm-runtime-wgl/src/platform/linux/iwasm_main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static char *uart_device = "/dev/ttyS2";
4545
static int baudrate = B115200;
4646
#endif
4747

48-
extern void
48+
extern bool
4949
init_sensor_framework();
5050
extern void
5151
exit_sensor_framework();
@@ -525,7 +525,9 @@ iwasm_main(int argc, char *argv[])
525525

526526
hal_init();
527527

528-
init_sensor_framework();
528+
if (!init_sensor_framework()) {
529+
goto fail2;
530+
}
529531

530532
// timer manager
531533
init_wasm_timer();
@@ -545,6 +547,8 @@ iwasm_main(int argc, char *argv[])
545547

546548
exit_wasm_timer();
547549
exit_sensor_framework();
550+
551+
fail2:
548552
wgl_exit();
549553
exit_connection_framework();
550554

samples/gui/wasm-runtime-wgl/src/platform/zephyr/iwasm_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "display.h"
1717
#include "lvgl.h"
1818

19-
extern void
19+
extern bool
2020
init_sensor_framework();
2121
extern void
2222
exit_sensor_framework();

samples/littlevgl/vgl-wasm-runtime/src/platform/linux/iwasm_main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static char *uart_device = "/dev/ttyS2";
4343
static int baudrate = B115200;
4444
#endif
4545

46-
extern void
46+
extern bool
4747
init_sensor_framework();
4848
extern void
4949
exit_sensor_framework();
@@ -505,7 +505,9 @@ iwasm_main(int argc, char *argv[])
505505
extern void display_SDL_init();
506506
display_SDL_init();
507507

508-
init_sensor_framework();
508+
if (!init_sensor_framework()) {
509+
goto fail2;
510+
}
509511

510512
// timer manager
511513
init_wasm_timer();
@@ -525,6 +527,8 @@ iwasm_main(int argc, char *argv[])
525527

526528
exit_wasm_timer();
527529
exit_sensor_framework();
530+
531+
fail2:
528532
exit_connection_framework();
529533

530534
fail1:

samples/littlevgl/vgl-wasm-runtime/src/platform/zephyr/iwasm_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <drivers/uart.h>
2121
#include <device.h>
2222

23-
extern void
23+
extern bool
2424
init_sensor_framework();
2525
extern void
2626
exit_sensor_framework();

samples/simple/src/iwasm_main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static char *uart_device = "/dev/ttyS2";
4545
static int baudrate = B115200;
4646
#endif
4747

48-
extern void
48+
extern bool
4949
init_sensor_framework();
5050
extern void
5151
exit_sensor_framework();
@@ -525,7 +525,10 @@ iwasm_main(int argc, char *argv[])
525525
}
526526

527527
/* sensor framework */
528-
init_sensor_framework();
528+
if (!init_sensor_framework()) {
529+
goto fail2;
530+
}
531+
529532
/* add the sys sensor objects */
530533
add_sys_sensor("sensor_test1", "This is a sensor for test", 0, 1000,
531534
read_test_sensor, config_test_sensor);
@@ -548,6 +551,8 @@ iwasm_main(int argc, char *argv[])
548551

549552
exit_wasm_timer();
550553
exit_sensor_framework();
554+
555+
fail2:
551556
exit_connection_framework();
552557

553558
fail1:

0 commit comments

Comments
 (0)