From 72d42d8e1df25c7eb737f4055c22b936b6e72533 Mon Sep 17 00:00:00 2001 From: Tedd Ho-Jeong An Date: Mon, 18 Oct 2021 10:28:31 -0700 Subject: [PATCH] mesh: Fix unchecked return value This patch fixes the unchecked return value(CWE-252) issues reported by the Coverity. Signed-off-by: Anuj Jain Signed-off-by: Ayush Garg --- mesh/keyring.c | 6 ++++-- mesh/mesh-io-unit.c | 6 ++++-- mesh/rpl.c | 22 ++++++++++++++-------- mesh/util.c | 11 ++++++++--- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/mesh/keyring.c b/mesh/keyring.c index 76a4090..fdef5d2 100644 --- a/mesh/keyring.c +++ b/mesh/keyring.c @@ -51,7 +51,8 @@ static int open_key_file(struct mesh_node *node, const char *key_dir, if (flags & O_CREAT) { snprintf(fname, PATH_MAX, "%s%s", node_path, key_dir); - mkdir(fname, 0755); + if (mkdir(fname, 0755) != 0) + l_error("Failed to create dir(%d): %s", errno, fname); } snprintf(fname, PATH_MAX, "%s%s/%3.3x", node_path, key_dir, idx); @@ -207,7 +208,8 @@ bool keyring_put_remote_dev_key(struct mesh_node *node, uint16_t unicast, snprintf(key_file, PATH_MAX, "%s%s", node_path, dev_key_dir); - mkdir(key_file, 0755); + if (mkdir(key_file, 0755) != 0) + l_error("Failed to create dir(%d): %s", errno, key_file); for (i = 0; i < count; i++) { snprintf(key_file, PATH_MAX, "%s%s/%4.4x", node_path, diff --git a/mesh/mesh-io-unit.c b/mesh/mesh-io-unit.c index 3b6abb0..400248d 100644 --- a/mesh/mesh-io-unit.c +++ b/mesh/mesh-io-unit.c @@ -133,7 +133,8 @@ static bool incoming(struct l_io *sio, void *user_data) buf[0] = 0; memcpy(buf + 1, pvt->unique_name, size + 1); - send(pvt->fd, buf, size + 2, MSG_DONTWAIT); + if (send(pvt->fd, buf, size + 2, MSG_DONTWAIT) < 0) + l_error("Failed to send(%d)", errno); } return true; @@ -304,7 +305,8 @@ static bool simple_match(const void *a, const void *b) static void send_pkt(struct mesh_io_private *pvt, struct tx_pkt *tx, uint16_t interval) { - send(pvt->fd, tx->pkt, tx->len, MSG_DONTWAIT); + if (send(pvt->fd, tx->pkt, tx->len, MSG_DONTWAIT) < 0) + l_error("Failed to send(%d)", errno); if (tx->delete) { l_queue_remove_if(pvt->tx_pkts, simple_match, tx); diff --git a/mesh/rpl.c b/mesh/rpl.c index 904cef7..3774263 100644 --- a/mesh/rpl.c +++ b/mesh/rpl.c @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -57,9 +58,10 @@ bool rpl_put_entry(struct mesh_node *node, uint16_t src, uint32_t iv_index, iv_index); dir = opendir(src_file); - if (!dir) - mkdir(src_file, 0755); - else + if (!dir) { + if (mkdir(src_file, 0755) != 0) + l_error("Failed to create dir: %s", src_file); + } else closedir(dir); snprintf(src_file, PATH_MAX, "%s%s/%8.8x/%4.4x", node_path, rpl_dir, @@ -81,8 +83,8 @@ bool rpl_put_entry(struct mesh_node *node, uint16_t src, uint32_t iv_index, iv_index--; snprintf(src_file, PATH_MAX, "%s%s/%8.8x/%4.4x", node_path, rpl_dir, iv_index, src); - remove(src_file); - + if (remove(src_file) < 0) + l_error("Failed to remove(%d): %s", errno, src_file); return result; } @@ -113,7 +115,9 @@ void rpl_del_entry(struct mesh_node *node, uint16_t src) if (entry->d_type == DT_DIR && entry->d_name[0] != '.') { snprintf(rpl_path, PATH_MAX, "%s%s/%s/%4.4x", node_path, rpl_dir, entry->d_name, src); - remove(rpl_path); + if (remove(rpl_path) < 0) + l_error("Failed to remove(%d): %s", errno, + rpl_path); } } @@ -254,7 +258,8 @@ void rpl_update(struct mesh_node *node, uint32_t cur) /* Make sure path exists */ snprintf(path, PATH_MAX, "%s%s", node_path, rpl_dir); - mkdir(path, 0755); + if (mkdir(path, 0755) != 0) + l_error("Failed to create dir(%d): %s", errno, path); dir = opendir(path); if (!dir) @@ -291,6 +296,7 @@ bool rpl_init(const char *node_path) return false; snprintf(path, PATH_MAX, "%s%s", node_path, rpl_dir); - mkdir(path, 0755); + if (mkdir(path, 0755) != 0) + l_error("Failed to create dir(%d): %s", errno, path); return true; } diff --git a/mesh/util.c b/mesh/util.c index 7a120e0..a45a293 100644 --- a/mesh/util.c +++ b/mesh/util.c @@ -14,6 +14,7 @@ #include +#include #include #include #include @@ -116,12 +117,14 @@ int create_dir(const char *dir_name) } strncat(dir, prev + 1, next - prev); - mkdir(dir, 0755); + if (mkdir(dir, 0755) != 0) + l_error("Failed to create dir(%d): %s", errno, dir); prev = next; } - mkdir(dir_name, 0755); + if (mkdir(dir_name, 0755) != 0) + l_error("Failed to create dir(%d): %s", errno, dir_name); return 0; } @@ -137,7 +140,9 @@ static int del_fobject(const char *fpath, const struct stat *sb, int typeflag, case FTW_SL: default: - remove(fpath); + if (remove(fpath) < 0) + l_error("Failed to remove(%d): %s", errno, fpath); + l_debug("RM %s", fpath); break; } -- 2.7.4