mesh: Fix unchecked return value
authorTedd Ho-Jeong An <tedd.an@intel.com>
Mon, 18 Oct 2021 17:28:31 +0000 (10:28 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:37 +0000 (19:08 +0530)
This patch fixes the unchecked return value(CWE-252) issues reported by
the Coverity.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
mesh/keyring.c
mesh/mesh-io-unit.c
mesh/rpl.c
mesh/util.c

index 76a4090..fdef5d2 100644 (file)
@@ -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,
index 3b6abb0..400248d 100644 (file)
@@ -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);
index 904cef7..3774263 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <errno.h>
 
 #include <sys/stat.h>
 
@@ -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;
 }
index 7a120e0..a45a293 100644 (file)
@@ -14,6 +14,7 @@
 
 
 #include <dirent.h>
+#include <errno.h>
 #include <ftw.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -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;
        }