From 487a738829cdbb726dfff8a8467a282990584741 Mon Sep 17 00:00:00 2001
From: Ludwig Krispenz <lkrispen@redhat.com>
Date: Tue, 20 Feb 2018 13:01:39 +0100
Subject: [PATCH] t49573 - coverity issues - part 1

---
 ldap/servers/plugins/replication/cl5_api.c              |  2 +-
 ldap/servers/plugins/replication/repl5_replica_config.c |  2 +-
 ldap/servers/plugins/replication/repl5_ruv.c            | 14 ++++++++------
 ldap/servers/plugins/replication/repl5_ruv.h            |  2 +-
 ldap/servers/slapd/task.c                               |  1 +
 5 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/ldap/servers/plugins/replication/cl5_api.c b/ldap/servers/plugins/replication/cl5_api.c
index 89ae995..5ceb90b 100644
--- a/ldap/servers/plugins/replication/cl5_api.c
+++ b/ldap/servers/plugins/replication/cl5_api.c
@@ -5426,7 +5426,7 @@ cl5BuildCSNList(const RUV *consRuv, const RUV *supRuv)
 
     PR_ASSERT(consRuv);
 
-    count = ruv_replica_count(consRuv);
+    count = ruv_replica_count(consRuv, PR_TRUE);
     csns = (CSN **)slapi_ch_calloc(count + 1, sizeof(CSN *));
 
     data.csns = csns;
diff --git a/ldap/servers/plugins/replication/repl5_replica_config.c b/ldap/servers/plugins/replication/repl5_replica_config.c
index ea430d9..2fa7e2a 100644
--- a/ldap/servers/plugins/replication/repl5_replica_config.c
+++ b/ldap/servers/plugins/replication/repl5_replica_config.c
@@ -1318,7 +1318,7 @@ replica_execute_cleanruv_task(Object *r, ReplicaId rid, char *returntext __attri
      *  - rid is not the last one
      */
     if ((replica_get_rid(replica) == rid) ||
-        (ruv_replica_count(local_ruv) <= 1)) {
+        (ruv_replica_count(local_ruv, PR_TRUE) <= 1)) {
         return LDAP_UNWILLING_TO_PERFORM;
     }
     rc = ruv_delete_replica(local_ruv, rid);
diff --git a/ldap/servers/plugins/replication/repl5_ruv.c b/ldap/servers/plugins/replication/repl5_ruv.c
index 7bfdc34..c1fc9b9 100644
--- a/ldap/servers/plugins/replication/repl5_ruv.c
+++ b/ldap/servers/plugins/replication/repl5_ruv.c
@@ -1367,16 +1367,16 @@ ruv_compare_ruv(const RUV *ruv1, const char *ruv1name, const RUV *ruv2, const ch
 }
 
 PRInt32
-ruv_replica_count(const RUV *ruv)
+ruv_replica_count(const RUV *ruv, PRBool lock_it)
 {
     if (ruv == NULL)
         return 0;
     else {
         int count;
 
-        slapi_rwlock_rdlock(ruv->lock);
+        if (lock_it) slapi_rwlock_rdlock(ruv->lock);
         count = dl_get_count(ruv->elements);
-        slapi_rwlock_unlock(ruv->lock);
+        if (lock_it) slapi_rwlock_unlock(ruv->lock);
 
         return count;
     }
@@ -1406,7 +1406,7 @@ ruv_get_referrals(const RUV *ruv)
 
     slapi_rwlock_rdlock(ruv->lock);
 
-    n = ruv_replica_count(ruv);
+    n = ruv_replica_count(ruv, PR_FALSE);
     if (n > 0) {
         RUVElement *replica;
         int cookie;
@@ -1666,8 +1666,10 @@ ruv_update_ruv(RUV *ruv, const CSN *csn, const char *replica_purl, void *replica
         repl_ruv = ruvGetReplica(ruv, prim_rid);
         rc = ruv_update_ruv_element(ruv, repl_ruv, prim_csn, replica_purl, PR_FALSE);
     }
-    repl_ruv = ruvGetReplica(ruv, local_rid);
-    rc = ruv_update_ruv_element(ruv, repl_ruv, prim_csn, replica_purl, PR_TRUE);
+    if (rc == RUV_SUCCESS) {
+        repl_ruv = ruvGetReplica(ruv, local_rid);
+        rc = ruv_update_ruv_element(ruv, repl_ruv, prim_csn, replica_purl, PR_TRUE);
+    }
     slapi_rwlock_unlock(ruv->lock);
     if (rc)
         return rc;
diff --git a/ldap/servers/plugins/replication/repl5_ruv.h b/ldap/servers/plugins/replication/repl5_ruv.h
index d002835..63b3fad 100644
--- a/ldap/servers/plugins/replication/repl5_ruv.h
+++ b/ldap/servers/plugins/replication/repl5_ruv.h
@@ -105,7 +105,7 @@ Slapi_Value **ruv_to_valuearray(RUV *ruv);
 int ruv_to_smod(const RUV *ruv, Slapi_Mod *smod);
 int ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod);
 int ruv_to_bervals(const RUV *ruv, struct berval ***bvals);
-PRInt32 ruv_replica_count(const RUV *ruv);
+PRInt32 ruv_replica_count(const RUV *ruv, PRBool lock_it);
 char **ruv_get_referrals(const RUV *ruv);
 void ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile);
 int ruv_add_csn_inprogress(void *repl, RUV *ruv, const CSN *csn);
diff --git a/ldap/servers/slapd/task.c b/ldap/servers/slapd/task.c
index 23a9f9e..c7ef3c2 100644
--- a/ldap/servers/slapd/task.c
+++ b/ldap/servers/slapd/task.c
@@ -599,6 +599,7 @@ new_task(const char *rawdn, void *plugin)
 
     if (task->task_log_lock == NULL) {
         /* Failed to allocate! Uh Oh! */
+        slapi_ch_free_string(&dn);
         slapi_ch_free((void **)&task);
         slapi_log_err(SLAPI_LOG_ERR, "new_task", "Unable to allocate task lock for: %s\n", rawdn);
         return NULL;
-- 
2.9.5

