add missing checks on so far ignored return values
authorSumit Bose <sbose@redhat.com>
Wed, 20 May 2009 10:08:13 +0000 (12:08 +0200)
committerRonnie Sahlberg <ronniesahlberg@gmail.com>
Thu, 21 May 2009 01:22:21 +0000 (11:22 +1000)
Most of these were found during a review by Jim Meyering <meyering@redhat.com>

(This used to be ctdb commit 3aee5ee1deb4a19be3bd3a4ce3abbe09de763344)

17 files changed:
ctdb/client/ctdb_client.c
ctdb/common/cmdline.c
ctdb/common/ctdb_util.c
ctdb/ib/ibw_ctdb.c
ctdb/ib/ibw_ctdb_init.c
ctdb/ib/ibwrapper.c
ctdb/ib/ibwrapper_test.c
ctdb/lib/util/db_wrap.c
ctdb/lib/util/debug.c
ctdb/server/ctdb_daemon.c
ctdb/server/ctdb_logging.c
ctdb/server/ctdb_server.c
ctdb/server/ctdb_takeover.c
ctdb/server/eventscript.c
ctdb/tests/src/ctdb_persistent.c
ctdb/tools/ctdb.c
ctdb/utils/ping_pong/ping_pong.c

index df9fa063551636bccdb997414d8a0b025b74b91a..7915a936ef2f805c67e46afbe21ea958f0227640 100644 (file)
@@ -2336,6 +2336,7 @@ int ctdb_ctrl_get_public_ipsv4(struct ctdb_context *ctdb,
        len = offsetof(struct ctdb_all_public_ips, ips) +
                ipsv4->num*sizeof(struct ctdb_public_ip);
        *ips = talloc_zero_size(mem_ctx, len);
+       CTDB_NO_MEMORY(ctdb, *ips);
        (*ips)->num = ipsv4->num;
        for (i=0; i<ipsv4->num; i++) {
                (*ips)->ips[i].pnn     = ipsv4->ips[i].pnn;
@@ -2708,14 +2709,24 @@ int ctdb_ctrl_get_server_id_list(struct ctdb_context *ctdb,
 */
 struct ctdb_context *ctdb_init(struct event_context *ev)
 {
+    int ret;
        struct ctdb_context *ctdb;
 
        ctdb = talloc_zero(ev, struct ctdb_context);
+    if (ctdb == NULL) {
+               DEBUG(DEBUG_ERR,(__location__ " talloc_zero failed.\n"));
+        return NULL;
+    }
        ctdb->ev  = ev;
        ctdb->idr = idr_init(ctdb);
        CTDB_NO_MEMORY_NULL(ctdb, ctdb->idr);
 
-       ctdb_set_socketname(ctdb, CTDB_PATH);
+       ret = ctdb_set_socketname(ctdb, CTDB_PATH);
+    if (ret != 0) {
+               DEBUG(DEBUG_ERR,(__location__ " ctdb_set_socketname failed.\n"));
+        talloc_free(ctdb);
+        return NULL;
+    }
 
        return ctdb;
 }
@@ -2735,6 +2746,7 @@ void ctdb_set_flags(struct ctdb_context *ctdb, unsigned flags)
 int ctdb_set_socketname(struct ctdb_context *ctdb, const char *socketname)
 {
        ctdb->daemon.name = talloc_strdup(ctdb, socketname);
+    if (ctdb->daemon.name == NULL) return -1;
        return 0;
 }
 
index 2136bf637b563e21bf93aa182eb50c0ec5a7c387..3acbf96e1301985e18f3a847d651c111610726f5 100644 (file)
@@ -117,7 +117,12 @@ struct ctdb_context *ctdb_cmdline_client(struct event_context *ev)
        /* tell ctdb the socket address */
        socket_name = getenv("CTDB_SOCKET");
        if (socket_name != NULL) {
-               ctdb_set_socketname(ctdb, socket_name);
+               ret = ctdb_set_socketname(ctdb, socket_name);
+               if (ret == -1) {
+                       printf("ctdb_set_socketname failed - %s\n",
+                                                   ctdb_errstr(ctdb));
+                       exit(1);
+               }
        }
 
        if (ctdb_cmdline.socketname != NULL) {
index 3604cc8ed4b7a543f2877bdf46b291967770208c..21810924d0ba5b59d8872d6cedc22cbefe236238 100644 (file)
@@ -73,6 +73,7 @@ int ctdb_parse_address(struct ctdb_context *ctdb,
        endservent();
        
        address->address = talloc_strdup(mem_ctx, str);
+       if (address->address == NULL) return -1;
        if (se == NULL) {
                address->port = CTDB_PORT;
        } else {
index fd577ffdce407315aeac8f33770c41dedd96e9cd..05541ada810215ed00c55306c2964c1c85e812f6 100644 (file)
@@ -166,6 +166,7 @@ int ctdb_ibw_receive_handler(struct ibw_conn *conn, void *buf, int n)
         * and being reused for next receive
         * noticed that HL requires talloc-ed memory to be stolen */
        buf2 = talloc_zero_size(conn, n);
+       if (buf2 == NULL) return -1;
        memcpy(buf2, buf, n);
 
        ctdb->upcalls->recv_pkt(ctdb, (uint8_t *)buf2, (uint32_t)n);
index 2f4fbfe3f263812775907f6e188262d275350ca7..fa2ddda5affdecbfff7031a1acab9d0632aee024 100644 (file)
@@ -164,7 +164,15 @@ static int ctdb_ibw_queue_pkt(struct ctdb_node *node, uint8_t *data, uint32_t le
                rc = ctdb_ibw_send_pkt(cn->conn, data, length);
        } else {
                struct ctdb_ibw_msg *p = talloc_zero(cn, struct ctdb_ibw_msg);
+               if (p == NULL) {
+                       DEBUG(DEBUG_ERR, ("talloc_zero failed.\n"));
+                       return -1;
+               }
                p->data = talloc_memdup(p, data, length);
+               if (p->data == NULL) {
+                       DEBUG(DEBUG_ERR, ("talloc_memdup failed.\n"));
+                       return -1;
+               }
                p->length = length;
 
                DLIST_ADD_AFTER(cn->queue, p, cn->queue_last);
index 5a166e5353f88588532f7f1c0f611f41dfaf7076..6a557cbb4de657fc0f79e394626ef2fe47c82657 100644 (file)
@@ -852,7 +852,7 @@ static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
                        }
 
                        if (part->to_read==0) {
-                               pctx->receive_func(conn, part->buf, part->len);
+                               if(pctx->receive_func(conn, part->buf, part->len)) goto error;
                                part->len = 0; /* tells not having partial data (any more) */
                                if (ibw_wc_mem_threshold(pconn, part, pctx->opts.recv_threshold))
                                        goto error;
@@ -867,7 +867,7 @@ static int ibw_wc_recv(struct ibw_conn *conn, struct ibv_wc *wc)
 
                                /* mostly awaited case: */
                                if (msglen<=remain) {
-                                       pctx->receive_func(conn, p, msglen);
+                                       if(pctx->receive_func(conn, p, msglen)) goto error;
                                        p += msglen;
                                        remain -= msglen;
                                } else {
index cbd4f43173819008bfa06b7bf16f5338a5dbdc7f..b9c80ae2ae2d001170b24b8ce3316ae1d7fcc998 100644 (file)
@@ -486,6 +486,7 @@ int ibwtest_getdests(struct ibwtest_ctx *tcx, char op)
        char    *tmp;
 
        tmp = talloc_strdup(tcx, optarg);
+       if (tmp == NULL) return -1;
        /* hack to reuse the above ibw_initattr parser */
        if (ibwtest_parse_attrs(tcx, tmp, &attrs, &tcx->naddrs, op))
                return -1;
@@ -567,6 +568,7 @@ int main(int argc, char *argv[])
                        break;
                case 'o':
                        tcx->opts = talloc_strdup(tcx, optarg);
+                       if (tcx->opts) goto cleanup;
                        if (ibwtest_parse_attrs(tcx, tcx->opts, &tcx->attrs,
                                &tcx->nattrs, op))
                                goto cleanup;
index f4e89cf270fc2472875a712068672f04e267f67b..7f5240e932891355ca2431b09ef40aa8130f4c8d 100644 (file)
@@ -82,6 +82,10 @@ struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
        }
 
        w->name = talloc_strdup(w, name);
+       if (w->name == NULL) {
+               talloc_free(w);
+               return NULL;
+       }
 
        w->tdb = tdb_open_ex(name, hash_size, tdb_flags, 
                             open_flags, mode, &log_ctx, NULL);
index 341a4a1586ab6534d079abb12e1366dd48ef5be2..9e819292200b374032f89904505efa7fa6622fb7 100644 (file)
@@ -28,8 +28,14 @@ static void _do_debug_v(const char *format, va_list ap)
        char *s = NULL;
        struct tm *tm;
        char tbuf[100];
-
-       vasprintf(&s, format, ap);
+    int ret;
+
+       ret = vasprintf(&s, format, ap);
+       if (ret == -1) {
+               fprintf(stderr, "vasprintf failed in _do_debug_v, cannot print debug message.\n");
+               fflush(stderr);
+               return;
+       }
 
        t = timeval_current();
        tm = localtime(&t.tv_sec);
index 8a3f564aa5df0f21f25a38336586043d67e000cf..957f5d8d11341355884ed3049aa1a61873167338 100644 (file)
@@ -690,6 +690,10 @@ int ctdb_start_daemon(struct ctdb_context *ctdb, bool do_fork)
 
        /* ensure the socket is deleted on exit of the daemon */
        domain_socket_name = talloc_strdup(talloc_autofree_context(), ctdb->daemon.name);
+       if (domain_socket_name == NULL) {
+               DEBUG(DEBUG_ALERT,(__location__ " talloc_strdup failed.\n"));
+               exit(12);
+       }
        talloc_set_destructor(domain_socket_name, unlink_destructor);   
 
        ctdb->ev = event_context_init(NULL);
index f9aa13a3c673c72506ac7186f3b0ac600d4b42a9..3700a33436b73d41f0666cff098e328c0b2cccad 100644 (file)
@@ -80,20 +80,29 @@ static void ctdb_logfile_log(const char *format, va_list ap)
        struct tm *tm;
        char tbuf[100];
        char *s2 = NULL;
+       int ret;
 
-       vasprintf(&s, format, ap);
+       ret = vasprintf(&s, format, ap);
+       if (ret == -1) {
+               write(log_state->fd, "vasprintf failed\n", strlen("vasprintf failed\n"));
+               return;
+       }
 
        t = timeval_current();
        tm = localtime(&t.tv_sec);
 
        strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
 
-       asprintf(&s2, "%s.%06u [%5u]: %s", 
+       ret = asprintf(&s2, "%s.%06u [%5u]: %s",
                 tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
        free(s);
+       if (ret == -1) {
+               write(log_state->fd, "asprintf failed\n", strlen("asprintf failed\n"));
+               return;
+       }
        if (s2) {
                write(log_state->fd, s2, strlen(s2));
-               free(s2);       
+               free(s2);
        }
 }
 
@@ -102,6 +111,7 @@ static void ctdb_logfile_log(const char *format, va_list ap)
 */
 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog)
 {
+       int ret;
        ctdb->log = talloc_zero(ctdb, struct ctdb_log_state);
        if (ctdb->log == NULL) {
                printf("talloc_zero failed\n");
@@ -117,7 +127,11 @@ int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_sy
                do_debug_v = ctdb_logfile_log;
                ctdb->log->fd = 1;
                /* also catch stderr of subcommands to stdout */
-               dup2(1, 2);
+               ret = dup2(1, 2);
+               if (ret == -1) {
+                       printf("dup2 failed: %s\n", strerror(errno));
+                       abort();
+               }
        } else {
                do_debug_v = ctdb_logfile_log;
 
@@ -193,6 +207,7 @@ static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
 int ctdb_set_child_logging(struct ctdb_context *ctdb)
 {
        int p[2];
+       int ret;
 
        if (ctdb->log->fd == 1) {
                /* not needed for stdout logging */
@@ -213,11 +228,19 @@ int ctdb_set_child_logging(struct ctdb_context *ctdb)
        close(1);
        close(2);
        if (p[1] != 1) {
-               dup2(p[1], 1);
+               ret = dup2(p[1], 1);
+               if (ret == -1) {
+                       printf("dup2 failed: %s\n", strerror(errno));
+                       return -1;
+               }
                close(p[1]);
        }
        /* also catch stderr of subcommands to the log */
-       dup2(1, 2);
+       ret = dup2(1, 2);
+       if (ret == -1) {
+               printf("dup2 failed: %s\n", strerror(errno));
+               return -1;
+       }
 
        return 0;
 }
index a701f753fbbe6cadabd61468ed916e868eb76ab3..aea73ecb06123509fa4616e1bfd63e3e775c9843 100644 (file)
@@ -31,6 +31,7 @@
 int ctdb_set_transport(struct ctdb_context *ctdb, const char *transport)
 {
        ctdb->transport = talloc_strdup(ctdb, transport);
+    if (ctdb->transport == NULL) return -1;
        return 0;
 }
 
@@ -57,6 +58,7 @@ int ctdb_ip_to_nodeid(struct ctdb_context *ctdb, const char *nodeip)
 int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file)
 {
        ctdb->recovery_lock_file = talloc_strdup(ctdb, file);
+    if (ctdb->recovery_lock_file == NULL) return -1;
        return 0;
 }
 
index 29c0ee6791122d560331b1c90d8e263378bed512..9eac660e8d9f57dce1e2af43c8200c6e0f2aa727 100644 (file)
@@ -326,6 +326,7 @@ static void release_ip_callback(struct ctdb_context *ctdb, int status,
           that the cluster has been reconfigured and they should
           release any sockets on this IP */
        data.dptr = (uint8_t *)talloc_strdup(state, ctdb_addr_to_str(state->addr));
+       CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
        data.dsize = strlen((char *)data.dptr)+1;
 
        DEBUG(DEBUG_INFO,(__location__ " sending RELEASE_IP for '%s'\n", data.dptr));
@@ -444,6 +445,7 @@ static int ctdb_add_public_address(struct ctdb_context *ctdb, ctdb_sock_addr *ad
        vnn = talloc_zero(ctdb, struct ctdb_vnn);
        CTDB_NO_MEMORY_FATAL(ctdb, vnn);
        vnn->iface = talloc_strdup(vnn, iface);
+       CTDB_NO_MEMORY(ctdb, vnn->iface);
        vnn->public_address      = *addr;
        vnn->public_netmask_bits = mask;
        vnn->pnn                 = -1;
index a30ac3f961c86fcdfb4c3af1c8039734bf98a316..14cd190693e66160323d37cf947bfb30464a7e4e 100644 (file)
@@ -151,6 +151,7 @@ int32_t ctdb_control_event_script_start(struct ctdb_context *ctdb, TDB_DATA inda
 
        script->next  = monitoring_status->scripts;
        script->name  = talloc_strdup(script, name);
+       CTDB_NO_MEMORY(ctdb, script->name);
        script->start = timeval_current();
        monitoring_status->scripts = script;
 
@@ -298,6 +299,7 @@ static int ctdb_event_script_v(struct ctdb_context *ctdb, const char *options)
        char *script;
        int count;
        int is_monitor = 0;
+       char *d_name_dup;
 
        if (!strcmp(options, "monitor")) {
                is_monitor = 1;
@@ -409,7 +411,9 @@ static int ctdb_event_script_v(struct ctdb_context *ctdb, const char *options)
                
                
                /* store the event script in the tree */
-               trbt_insert32(tree, (num<<16)|count++, talloc_strdup(tree, de->d_name));
+               d_name_dup = talloc_strdup(tree, de->d_name);
+               CTDB_NO_MEMORY(ctdb, d_name_dup);
+               trbt_insert32(tree, (num<<16)|count++, d_name_dup);
        }
        closedir(dir);
 
index 8f0452abad492775c09ca687dbb771c6137a7677..325b378ffa3de454f101466850d6333f232c6bb6 100644 (file)
@@ -71,6 +71,7 @@ static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
 {
        int i;
        uint32_t *counters, *old_counters;
+    unsigned char *tmp_dptr;
 
        counters     = (uint32_t *)data.dptr;
        old_counters = (uint32_t *)old_data.dptr;
@@ -86,7 +87,14 @@ static void check_counters(struct ctdb_context *ctdb, TDB_DATA data)
 
        if (old_data.dsize != data.dsize) {
                old_data.dsize = data.dsize;
-               old_data.dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
+               tmp_dptr = talloc_realloc_size(ctdb, old_data.dptr, old_data.dsize);
+               if (tmp_dptr == NULL) {
+                       printf("[%4u] ERROR: talloc_realloc_size failed.\n", getpid());
+                       success = false;
+                       return;
+               } else {
+                       old_data.dptr = tmp_dptr;
+               }
        }
 
        memcpy(old_data.dptr, data.dptr, data.dsize);
index b85354c001930dca9d29fe7ff07b7d24186e466c..57e1a8dffe351e40694578649c50a7ce342ed00b 100644 (file)
@@ -424,6 +424,7 @@ static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
                pnn_node = talloc(mem_ctx, struct pnn_node);
                pnn_node->pnn = pnn++;
                pnn_node->addr = talloc_strdup(pnn_node, node);
+               CTDB_NO_MEMORY(ctdb, pnn_node->addr);
                pnn_node->next = pnn_nodes;
                pnn_nodes = pnn_node;
        }
@@ -597,6 +598,7 @@ static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **a
                }
                natgw_node = talloc(ctdb, struct natgw_node);
                natgw_node->addr = talloc_strdup(natgw_node, node);
+               CTDB_NO_MEMORY(ctdb, natgw_node->addr);
                natgw_node->next = natgw_nodes;
                natgw_nodes = natgw_node;
        }
@@ -2364,7 +2366,8 @@ static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **ar
        struct db_file_header dbhdr;
        struct ctdb_db_context *ctdb_db;
        struct backup_data *bd;
-       int fh;
+       int fh = -1;
+       int status = -1;
 
        if (argc != 2) {
                DEBUG(DEBUG_ERR,("Invalid arguments\n"));
@@ -2397,6 +2400,7 @@ static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **ar
        ctdb_db = ctdb_attach(ctdb, argv[0], dbmap->dbs[i].persistent, 0);
        if (ctdb_db == NULL) {
                DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
+               talloc_free(tmp_ctx);
                return -1;
        }
 
@@ -2449,16 +2453,30 @@ static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **ar
        dbhdr.size = bd->len;
        if (strlen(argv[0]) >= MAX_DB_NAME) {
                DEBUG(DEBUG_ERR,("Too long dbname\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               goto done;
        }
        strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
-       write(fh, &dbhdr, sizeof(dbhdr));
-       write(fh, bd->records, bd->len);
+       ret = write(fh, &dbhdr, sizeof(dbhdr));
+       if (ret == -1) {
+               DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
+               goto done;
+       }
+       ret = write(fh, bd->records, bd->len);
+       if (ret == -1) {
+               DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
+               goto done;
+       }
 
-       close(fh);
+       status = 0;
+done:
+       if (fh != -1) {
+               ret = close(fh);
+               if (ret == -1) {
+                       DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
+               }
+       }
        talloc_free(tmp_ctx);
-       return 0;
+       return status;
 }
 
 /*
index 90692e59ecc51f2751d34664d96e5e4f60047c92..27ff0577ac103b6c160cf5b32d4a521e01ac4367 100644 (file)
@@ -92,16 +92,29 @@ static void ping_pong(int fd, int num_locks)
        unsigned char *val;
        unsigned char incr=0, last_incr=0;
        unsigned char *p = NULL;
+       int ret;
 
-       ftruncate(fd, num_locks+1);
+       ret = ftruncate(fd, num_locks+1);
+       if (ret == -1) {
+               printf("ftruncate failed: %s\n", strerror(errno));
+               return;
+       }
 
        if (use_mmap) {
                p = mmap(NULL, num_locks+1, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+               if (p == MAP_FAILED) {
+                       printf("mmap failed: %s\n", strerror(errno));
+                       return;
+               }
        }
 
        val = (unsigned char *)calloc(num_locks+1, sizeof(unsigned char));
+       if (val == NULL) {
+               printf("calloc failed\n");
+               return;
+       }
 
-       start_timer();  
+       start_timer();
 
        lock_range(fd, 0, 1);
        i = 0;