[PATCH 1/5] ipcpd: Fix some unchecked return values

  • From: Dimitri Staessens <dimitri@ouroboros.rocks>
  • To: ouroboros@xxxxxxxxxxxxx
  • Date: Fri, 25 Feb 2022 00:15:28 +0100

Fixes some unchecked and wrongly checked return values.

Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks>
---
 src/ipcpd/local/main.c      | 28 ++++++++++++++++------------
 src/ipcpd/shim-data.c       | 22 ++++++++++++++++++----
 src/ipcpd/udp/main.c        |  2 +-
 src/ipcpd/unicast/dir/dht.c | 17 ++++++++++-------
 4 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/src/ipcpd/local/main.c b/src/ipcpd/local/main.c
index 9c62c3cc..c0e0b702 100644
--- a/src/ipcpd/local/main.c
+++ b/src/ipcpd/local/main.c
@@ -72,24 +72,28 @@ static int local_data_init(void)
 
         local_data.flows = fset_create();
         if (local_data.flows == NULL)
-                return -ENFILE;
+                goto fail_fset;
 
         local_data.fq = fqueue_create();
-        if (local_data.fq == NULL) {
-                fset_destroy(local_data.flows);
-                return -ENOMEM;
-        }
+        if (local_data.fq == NULL)
+                goto fail_fqueue;
 
         local_data.shim_data = shim_data_create();
-        if (local_data.shim_data == NULL) {
-                fqueue_destroy(local_data.fq);
-                fset_destroy(local_data.flows);
-                return -ENOMEM;
-        }
-
-        pthread_rwlock_init(&local_data.lock, NULL);
+        if (local_data.shim_data == NULL)
+                goto fail_shim_data;
 
+        if (pthread_rwlock_init(&local_data.lock, NULL) < 0)
+                goto fail_rwlock_init;
         return 0;
+
+ fail_rwlock_init:
+        shim_data_destroy(local_data.shim_data);
+ fail_shim_data:
+        fqueue_destroy(local_data.fq);
+ fail_fqueue:
+        fset_destroy(local_data.flows);
+ fail_fset:
+        return -ENOMEM;
 }
 
 static void local_data_fini(void){
diff --git a/src/ipcpd/shim-data.c b/src/ipcpd/shim-data.c
index ade157ce..761ecc3b 100644
--- a/src/ipcpd/shim-data.c
+++ b/src/ipcpd/shim-data.c
@@ -141,7 +141,9 @@ static void dir_entry_destroy(struct dir_entry * entry)
 
 struct shim_data * shim_data_create()
 {
-        struct shim_data * sd = malloc(sizeof(*sd));
+        struct shim_data * sd;
+
+        sd = malloc(sizeof(*sd));
         if (sd == NULL)
                 return NULL;
 
@@ -151,11 +153,23 @@ struct shim_data * shim_data_create()
         list_head_init(&sd->dir_queries);
 
         /* init the locks */
-        pthread_rwlock_init(&sd->reg_lock, NULL);
-        pthread_rwlock_init(&sd->dir_lock, NULL);
-        pthread_mutex_init(&sd->dir_queries_lock, NULL);
+        if (pthread_rwlock_init(&sd->reg_lock, NULL) < 0)
+                goto fail_reg_lock_init;
+
+        if (pthread_rwlock_init(&sd->dir_lock, NULL) < 0)
+                goto fail_dir_lock_init;
+
+        if (pthread_mutex_init(&sd->dir_queries_lock, NULL) < 0)
+                goto fail_mutex_init;
 
         return sd;
+
+ fail_mutex_init:
+        pthread_rwlock_destroy(&sd->dir_lock);
+ fail_dir_lock_init:
+        pthread_rwlock_destroy(&sd->reg_lock);
+ fail_reg_lock_init:
+        return NULL;
 }
 
 static void clear_registry(struct shim_data * data)
diff --git a/src/ipcpd/udp/main.c b/src/ipcpd/udp/main.c
index e6caea45..b9f97e74 100644
--- a/src/ipcpd/udp/main.c
+++ b/src/ipcpd/udp/main.c
@@ -776,7 +776,7 @@ static uint32_t ddns_resolve(char *   name,
 
         waitpid(pid, &wstatus, 0);
         if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) == 0 &&
-            count != IPCP_UDP_BUF_SIZE)
+            count != IPCP_UDP_BUF_SIZE - 1)
                 log_dbg("Succesfully communicated with nslookup.");
         else
                 log_err("Failed to resolve DNS address.");
diff --git a/src/ipcpd/unicast/dir/dht.c b/src/ipcpd/unicast/dir/dht.c
index d84da9da..572010b7 100644
--- a/src/ipcpd/unicast/dir/dht.c
+++ b/src/ipcpd/unicast/dir/dht.c
@@ -1269,7 +1269,7 @@ static void bucket_refresh(struct dht *       dht,
                 struct contact * d;
                 c = list_first_entry(&b->contacts, struct contact, next);
                 d = contact_create(c->id, dht->b, c->addr);
-                if (c != NULL)
+                if (d != NULL)
                         list_add(&d->next, r);
                 return;
         }
@@ -1912,11 +1912,11 @@ static buffer_t dht_retrieve(struct dht *    dht,
         if (buf.len == 0)
                 goto fail;
 
-        buf.data = malloc(sizeof(dht->addr) * buf.len);
-        if (buf.data == NULL)
+        pos = malloc(sizeof(dht->addr) * buf.len);
+        if (pos == NULL)
                 goto fail;
 
-        pos = (uint64_t *) buf.data;
+        buf.data = (uint8_t *) pos;
 
         list_for_each(p, &e->vals) {
                 struct val * v = list_entry(p, struct val, next);
@@ -1931,8 +1931,8 @@ static buffer_t dht_retrieve(struct dht *    dht,
 
  fail:
         pthread_rwlock_unlock(&dht->lock);
-        buf.len = 0;
-
+        buf.len  = 0;
+        buf.data = NULL;
         return buf;
 }
 
@@ -2844,7 +2844,8 @@ void * dht_create(void)
         if ((int) dht->eid < 0)
                 goto fail_tpm_start;
 
-        notifier_reg(handle_event, dht);
+        if (notifier_reg(handle_event, dht))
+                goto fail_notifier_reg;
 #else
         (void) handle_event;
         (void) dht_handle_packet;
@@ -2854,6 +2855,8 @@ void * dht_create(void)
 
         return (void *) dht;
 #ifndef __DHT_TEST__
+ fail_notifier_reg:
+        tpm_stop(dht->tpm);
  fail_tpm_start:
         tpm_destroy(dht->tpm);
  fail_tpm_create:
-- 
2.35.1


Other related posts:

  • » [PATCH 1/5] ipcpd: Fix some unchecked return values - Dimitri Staessens