Fixes for various issues found by Coverity
authorSumit Bose <sbose@redhat.com>
Wed, 10 Aug 2011 15:53:56 +0000 (17:53 +0200)
committerMichael Adam <obnox@samba.org>
Mon, 19 Aug 2013 09:34:27 +0000 (11:34 +0200)
Corresponds to commit 05bfdbbd0d4abdfbcf28e3930086723508b35952 from master.

client/ctdb_client.c
common/ctdb_logging.c
server/ctdb_daemon.c
server/ctdb_logging.c
server/ctdb_ltdb_server.c
server/ctdb_recoverd.c
server/eventscript.c
tcp/tcp_connect.c
tests/src/ctdb_fetch.c

index c1b79af5eaf3a777d79087bebbd80e43e751a0f0..25eabc6e7d0587915894e085c94d2bdac14744c5 100644 (file)
@@ -3071,7 +3071,7 @@ static void async_callback(struct ctdb_client_control_state *state)
        struct ctdb_context *ctdb = talloc_get_type(state->ctdb, struct ctdb_context);
        int ret;
        TDB_DATA outdata;
-       int32_t res;
+       int32_t res = -1;
        uint32_t destnode = state->c->hdr.destnode;
 
        /* one more node has responded with recmode data */
index ea4d2716302e8abd2281bd76db1a0dcdc550ec68..8fd90b487706e6137b2c690393d8ffe3219cc1d1 100644 (file)
@@ -124,7 +124,7 @@ static void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr
                tm = localtime(&log_entries[tmp_entry].t.tv_sec);
                strftime(tbuf, sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
 
-               if (log_entries[tmp_entry].message) {
+               if (log_entries[tmp_entry].message[0] != '\0') {
                        count += fprintf(f, "%s:%s %s", tbuf, get_debug_by_level(log_entries[tmp_entry].level), log_entries[tmp_entry].message);
                }
 
@@ -135,9 +135,17 @@ static void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr
        }
 
        fsize = ftell(f);
+       if (fsize < 0) {
+               fclose(f);
+               DEBUG(DEBUG_ERR,("Cannot get current file position\n"));
+                return;
+       }
        rewind(f);
        data.dptr = talloc_size(NULL, fsize);
-       CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
+       if (data.dptr == NULL) {
+               fclose(f);
+               CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
+       }
        data.dsize = fread(data.dptr, 1, fsize, f);
        fclose(f);
 
index 1876f56bddfb96f8c8737a1fee6c3d0eb9885665..37a756b07ce0f509dc403cceb67c60c37f4d17ef 100644 (file)
@@ -822,6 +822,10 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork, bool use_syslog)
        fde = event_add_fd(ctdb->ev, ctdb, ctdb->daemon.sd, 
                           EVENT_FD_READ|EVENT_FD_AUTOCLOSE, 
                           ctdb_accept_client, ctdb);
+       if (fde == NULL) {
+               DEBUG(DEBUG_CRIT,("Failed to add daemon socket to event loop\n"));
+               exit(1);
+       }
 
        /* release any IPs we hold from previous runs of the daemon */
        ctdb_release_all_ips(ctdb);
index 7881c2f2b154ad030750df6aec35672812f0baf9..eb155cab6cb71100827b142230670382ee1f6c85 100644 (file)
@@ -502,6 +502,10 @@ int ctdb_set_child_logging(struct ctdb_context *ctdb)
        /* We'll fail if stderr/stdout not already open; it's simpler. */
        old_stdout = dup(STDOUT_FILENO);
        old_stderr = dup(STDERR_FILENO);
+        if (old_stdout < 0 || old_stderr < 0) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to copy files descriptors\n"));
+               return -1;
+        }
        if (dup2(p[1], STDOUT_FILENO) < 0 || dup2(p[1], STDERR_FILENO) < 0) {
                int saved_errno = errno;
                dup2(old_stdout, STDOUT_FILENO);
index 7cc06fc2b7e5465d1bb5d1f36245fb66994c954c..14709320603c2b5f4166af5429e5456642836114 100644 (file)
@@ -1042,7 +1042,10 @@ static int ctdb_attach_persistent(struct ctdb_context *ctdb,
                int invalid_name = 0;
                
                s = talloc_strdup(ctdb, de->d_name);
-               CTDB_NO_MEMORY(ctdb, s);
+               if (s == NULL) {
+                       closedir(d);
+                       CTDB_NO_MEMORY(ctdb, s);
+               }
 
                /* only accept names ending in .tdb */
                p = strstr(s, ".tdb.");
index 9bd7e95db41ba2793a82252a31acc586f4b7d0c6..db2a2caa27d17abfb37c058548bd52ce9e1aaa04 100644 (file)
@@ -1079,8 +1079,8 @@ static int traverse_recdb(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
        }
        params->recdata = talloc_realloc_size(NULL, params->recdata, rec->length + params->len);
        if (params->recdata == NULL) {
-               DEBUG(DEBUG_CRIT,(__location__ " Failed to expand recdata to %u (%u records)\n", 
-                        rec->length + params->len, params->recdata->count));
+               DEBUG(DEBUG_CRIT,(__location__ " Failed to expand recdata to %u\n",
+                        rec->length + params->len));
                params->failed = true;
                return -1;
        }
index 757b2546a342781dfa42582488feaf361a91b3d1..182aed61b5bd89be74ded616ee9600f716cd68d6 100644 (file)
@@ -222,6 +222,7 @@ static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb,
                tree_item = talloc(tree, struct ctdb_script_tree_item);
                if (tree_item == NULL) {
                        DEBUG(DEBUG_ERR, (__location__ " Failed to allocate new tree item\n"));
+                       closedir(dir);
                        talloc_free(tmp_ctx);
                        return NULL;
                }
@@ -234,6 +235,7 @@ static struct ctdb_scripts_wire *ctdb_get_script_list(struct ctdb_context *ctdb,
                tree_item->name = talloc_strdup(tree_item, de->d_name);
                if (tree_item->name == NULL) {
                        DEBUG(DEBUG_ERR,(__location__ " Failed to allocate script name.\n"));
+                       closedir(dir);
                        talloc_free(tmp_ctx);
                        return NULL;
                }
@@ -777,10 +779,10 @@ int ctdb_event_script_args(struct ctdb_context *ctdb, enum ctdb_eventscript_call
        va_start(ap, fmt);
        ret = ctdb_event_script_callback_v(ctdb,
                        event_script_callback, &status, false, call, fmt, ap);
+       va_end(ap);
        if (ret != 0) {
                return ret;
        }
-       va_end(ap);
 
        status.status = -1;
        status.done = false;
index 43ce85019bf3f9f8d758ec9d0e12c71fe7ec6000..e3d837af383a4a1fb7986b8f9c804fc4b5a9ceb5 100644 (file)
@@ -155,6 +155,10 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
        }
 
        tnode->fd = socket(sock_out.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
+       if (tnode->fd == -1) {
+               DEBUG(DEBUG_ERR, (__location__ " Failed to create socket.\n"));
+               return;
+       }
        set_nonblocking(tnode->fd);
        set_close_on_exec(tnode->fd);
 
@@ -197,7 +201,11 @@ void ctdb_tcp_node_connect(struct event_context *ev, struct timed_event *te,
        sock_in.ip.sin_len = sockin_size;
        sock_out.ip.sin_len = sockout_size;
 #endif
-       bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size);
+       if (bind(tnode->fd, (struct sockaddr *)&sock_in, sockin_size) != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to bind() to socket. %s(%d)\n", strerror(errno), errno));
+               close(tnode->fd);
+               return;
+       }
 
        if (connect(tnode->fd, (struct sockaddr *)&sock_out, sockout_size) != 0 &&
            errno != EINPROGRESS) {
index 35257e2fe5c4a0ff44687dbed11ddf264da0e7b8..6487dc0a595aedfd54b63c7d0be7529eeb4c41b4 100644 (file)
@@ -84,6 +84,11 @@ static void bench_fetch_1node(struct ctdb_context *ctdb)
        data.dptr = (uint8_t *)talloc_asprintf_append((char *)data.dptr, 
                                                      "msg_count=%d on node %d\n",
                                                      msg_count, ctdb_get_pnn(ctdb));
+       if (data.dptr == NULL) {
+               printf("Failed to create record\n");
+               talloc_free(tmp_ctx);
+               return;
+       }
        data.dsize = strlen((const char *)data.dptr)+1;
 
        ret = ctdb_record_store(h, data);