common: Coverity fixes
authorAmitay Isaacs <amitay@gmail.com>
Mon, 11 Nov 2013 01:40:28 +0000 (12:40 +1100)
committerAmitay Isaacs <amitay@gmail.com>
Thu, 14 Nov 2013 03:39:14 +0000 (14:39 +1100)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
common/ctdb_ltdb.c
common/ctdb_util.c
common/system_linux.c

index 4681f304187f53835e7e42fb917cdad1f9077c17..500f7211dfbc00f570ae602daf77790c3c9e8e97 100644 (file)
@@ -98,7 +98,10 @@ int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db,
                        *data = d2;
                }
                if (ctdb_db->persistent || header->dmaster == ctdb_db->ctdb->pnn) {
-                       ctdb_ltdb_store(ctdb_db, key, header, d2);
+                       if (ctdb_ltdb_store(ctdb_db, key, header, d2) != 0) {
+                               DEBUG(DEBUG_NOTICE,
+                                     (__location__ "failed to store initial header\n"));
+                       }
                }
                return 0;
        }
index 7a70fea9cd71c1c51eeedff6d3c68a294d1daff4..44eb0db48b0d7862d61c0977737c99ed7b85a0f7 100644 (file)
@@ -415,16 +415,34 @@ void ctdb_restore_scheduler(struct ctdb_context *ctdb)
 
 void set_nonblocking(int fd)
 {
-       unsigned v;
+       int v;
+
        v = fcntl(fd, F_GETFL, 0);
-        fcntl(fd, F_SETFL, v | O_NONBLOCK);
+       if (v == -1) {
+               DEBUG(DEBUG_WARNING, ("Failed to get file status flags - %s\n",
+                                     strerror(errno)));
+               return;
+       }
+        if (fcntl(fd, F_SETFL, v | O_NONBLOCK) == -1) {
+               DEBUG(DEBUG_WARNING, ("Failed to set non_blocking on fd - %s\n",
+                                     strerror(errno)));
+       }
 }
 
 void set_close_on_exec(int fd)
 {
-       unsigned v;
+       int v;
+
        v = fcntl(fd, F_GETFD, 0);
-       fcntl(fd, F_SETFD, v | FD_CLOEXEC);
+       if (v == -1) {
+               DEBUG(DEBUG_WARNING, ("Failed to get file descriptor flags - %s\n",
+                                     strerror(errno)));
+               return;
+       }
+       if (fcntl(fd, F_SETFD, v | FD_CLOEXEC) != 0) {
+               DEBUG(DEBUG_WARNING, ("Failed to set close_on_exec on fd - %s\n",
+                                     strerror(errno)));
+       }
 }
 
 
@@ -821,7 +839,7 @@ void ctdb_mkdir_p_or_die(struct ctdb_context *ctdb, const char *dir, int mode)
                DEBUG(DEBUG_ALERT,
                      ("ctdb exiting with error: "
                       "failed to create directory \"%s\" (%s)\n",
-                      dir, strerror(ret)));
+                      dir, strerror(errno)));
                exit(1);
        }
 }
index 84daba4fdfae21916b121eaa43201bebc5f004dc..9aaa1fdd7cea8ab25d292e70f435e4432a65b71e 100644 (file)
@@ -83,6 +83,8 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
        struct ifreq ifr;
 
        ZERO_STRUCT(sall);
+       ZERO_STRUCT(ifr);
+       ZERO_STRUCT(if_hwaddr);
 
        switch (addr->ip.sin_family) {
        case AF_INET:
@@ -93,7 +95,7 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                }
 
                DEBUG(DEBUG_DEBUG, (__location__ " Created SOCKET FD:%d for sending arp\n", s));
-               strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
+               strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)-1);
                if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
                        DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface));
                        close(s);
@@ -101,7 +103,7 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                }
 
                /* get the mac address */
-               strcpy(if_hwaddr.ifr_name, iface);
+               strncpy(if_hwaddr.ifr_name, iface, sizeof(if_hwaddr.ifr_name)-1);
                ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
                if ( ret < 0 ) {
                        close(s);
@@ -195,7 +197,7 @@ int ctdb_sys_send_arp(const ctdb_sock_addr *addr, const char *iface)
                }
 
                /* get the mac address */
-               strcpy(if_hwaddr.ifr_name, iface);
+               strncpy(if_hwaddr.ifr_name, iface, sizeof(if_hwaddr.ifr_name)-1);
                ret = ioctl(s, SIOCGIFHWADDR, &if_hwaddr);
                if ( ret < 0 ) {
                        close(s);
@@ -554,7 +556,7 @@ bool ctdb_sys_check_iface_exists(const char *iface)
                return true;
        }
 
-       strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name));
+       strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)-1);
        if (ioctl(s, SIOCGIFINDEX, &ifr) < 0 && errno == ENODEV) {
                DEBUG(DEBUG_CRIT,(__location__ " interface '%s' not found\n", iface));
                close(s);
@@ -587,7 +589,7 @@ char *ctdb_get_process_name(pid_t pid)
        int n;
 
        snprintf(path, sizeof(path), "/proc/%d/exe", pid);
-       n = readlink(path, buf, sizeof(buf));
+       n = readlink(path, buf, sizeof(buf)-1);
        if (n < 0) {
                return NULL;
        }
@@ -595,7 +597,7 @@ char *ctdb_get_process_name(pid_t pid)
        /* Remove any extra fields */
        buf[n] = '\0';
        ptr = strtok(buf, " ");
-       return strdup(ptr);
+       return (ptr == NULL ? ptr : strdup(ptr));
 }
 
 /*
@@ -701,14 +703,13 @@ bool ctdb_get_lock_info(pid_t req_pid, struct ctdb_lock_info *lock_info)
        struct ctdb_lock_info curlock;
        pid_t pid;
        char buf[1024];
-       char *ptr;
        bool status = false;
 
        if ((fp = fopen("/proc/locks", "r")) == NULL) {
                DEBUG(DEBUG_ERR, ("Failed to read locks information"));
                return false;
        }
-       while ((ptr = fgets(buf, sizeof(buf), fp)) != NULL) {
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
                if (! parse_proc_locks_line(buf, &pid, &curlock)) {
                        continue;
                }
@@ -733,14 +734,13 @@ bool ctdb_get_blocker_pid(struct ctdb_lock_info *reqlock, pid_t *blocker_pid)
        struct ctdb_lock_info curlock;
        pid_t pid;
        char buf[1024];
-       char *ptr;
        bool status = false;
 
        if ((fp = fopen("/proc/locks", "r")) == NULL) {
                DEBUG(DEBUG_ERR, ("Failed to read locks information"));
                return false;
        }
-       while ((ptr = fgets(buf, sizeof(buf), fp)) != NULL) {
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
                if (! parse_proc_locks_line(buf, &pid, &curlock)) {
                        continue;
                }