Blame SOURCES/0002-Ticket-49385-Fix-coverity-warnings.patch

b045b9
From 8308e20075adacfdf1827aaa3230e503207832bc Mon Sep 17 00:00:00 2001
b045b9
From: Mark Reynolds <mreynolds@redhat.com>
b045b9
Date: Mon, 2 Oct 2017 09:33:29 -0400
b045b9
Subject: [PATCH] Ticket 49385 - Fix coverity warnings
b045b9
b045b9
Description:  This fixes coverity issues found from RHEL build of 1.3.7
b045b9
b045b9
https://pagure.io/389-ds-base/issue/49385
b045b9
b045b9
Reviewed by: lkrispenz(Thanks!)
b045b9
b045b9
(cherry picked from commit 805e8f4d3016eb9c7906c1416482047a234d51ba)
b045b9
---
b045b9
 ldap/servers/plugins/http/http_impl.c    |  1 +
b045b9
 ldap/servers/plugins/replication/urp.c   | 10 ++++++----
b045b9
 ldap/servers/plugins/syntaxes/string.c   |  1 +
b045b9
 ldap/servers/slapd/back-ldbm/dbversion.c |  2 +-
b045b9
 ldap/servers/slapd/back-ldbm/index.c     |  1 +
b045b9
 ldap/servers/slapd/conntable.c           |  2 +-
b045b9
 ldap/servers/slapd/modify.c              |  1 +
b045b9
 ldap/servers/slapd/plugin.c              |  3 ++-
b045b9
 ldap/servers/slapd/referral.c            | 18 +++++++++---------
b045b9
 ldap/servers/slapd/task.c                |  8 ++++++--
b045b9
 ldap/servers/snmp/main.c                 | 13 +++++++++++--
b045b9
 11 files changed, 40 insertions(+), 20 deletions(-)
b045b9
b045b9
diff --git a/ldap/servers/plugins/http/http_impl.c b/ldap/servers/plugins/http/http_impl.c
b045b9
index d8bbe38..350c839 100644
b045b9
--- a/ldap/servers/plugins/http/http_impl.c
b045b9
+++ b/ldap/servers/plugins/http/http_impl.c
b045b9
@@ -601,6 +601,7 @@ sendPostReq(PRFileDesc *fd, const char *path, httpheader **httpheaderArray, char
b045b9
     if (path) {
b045b9
         path_len = strlen(path);
b045b9
     } else {
b045b9
+        path = "";
b045b9
         path_len = 0;
b045b9
     }
b045b9
 
b045b9
diff --git a/ldap/servers/plugins/replication/urp.c b/ldap/servers/plugins/replication/urp.c
b045b9
index 3d63c64..9534c03 100644
b045b9
--- a/ldap/servers/plugins/replication/urp.c
b045b9
+++ b/ldap/servers/plugins/replication/urp.c
b045b9
@@ -856,7 +856,7 @@ urp_post_delete_operation(Slapi_PBlock *pb)
b045b9
 static int
b045b9
 urp_fixup_add_cenotaph (Slapi_PBlock *pb, char *sessionid, CSN *opcsn)
b045b9
 {
b045b9
-    Slapi_PBlock *add_pb = slapi_pblock_new();
b045b9
+    Slapi_PBlock *add_pb;
b045b9
     Slapi_Entry *cenotaph = NULL;
b045b9
     Slapi_Entry *pre_entry = NULL;
b045b9
     int ret = 0;
b045b9
@@ -886,6 +886,7 @@ urp_fixup_add_cenotaph (Slapi_PBlock *pb, char *sessionid, CSN *opcsn)
b045b9
     slapi_rdn_remove_attr (rdn, SLAPI_ATTR_UNIQUEID );
b045b9
     slapi_rdn_add(rdn, "cenotaphID", uniqueid);
b045b9
     newdn = slapi_ch_smprintf("%s,%s", slapi_rdn_get_rdn(rdn), parentdn);
b045b9
+    slapi_rdn_free(&rdn;;
b045b9
     slapi_ch_free_string(&parentdn);
b045b9
     /* slapi_sdn_free(&pre_sdn); */
b045b9
 
b045b9
@@ -902,6 +903,7 @@ urp_fixup_add_cenotaph (Slapi_PBlock *pb, char *sessionid, CSN *opcsn)
b045b9
 
b045b9
     slapi_log_err(SLAPI_LOG_REPL, sessionid,
b045b9
                    "urp_fixup_add_cenotaph - addinng cenotaph: %s \n", newdn);
b045b9
+    add_pb = slapi_pblock_new();
b045b9
     slapi_pblock_init(add_pb);
b045b9
 
b045b9
     slapi_add_entry_internal_set_pb(add_pb,
b045b9
@@ -1661,8 +1663,8 @@ urp_conflict_to_glue (char *sessionid, const Slapi_Entry *entry, Slapi_DN *paren
b045b9
                            "urp_conflict_to_glue failed(%d) - %s --> %s\n", op_result, basedn, newrdn);
b045b9
             rc = 1;
b045b9
         }
b045b9
-        slapi_ch_free ( (void**)&newrdn );
b045b9
     }
b045b9
+    slapi_rdn_free(&parentrdn);
b045b9
     return rc;
b045b9
 }
b045b9
 /* 
b045b9
@@ -2166,11 +2168,11 @@ mod_objectclass_attr(const char *uniqueid, const Slapi_DN *entrysdn, const Slapi
b045b9
 {
b045b9
     Slapi_Mods smods;
b045b9
     int op_result;
b045b9
-    char csnstr[CSN_STRSIZE+1];
b045b9
+    char csnstr[CSN_STRSIZE+1] = {0};
b045b9
 
b045b9
     slapi_mods_init(&smods, 3);
b045b9
     slapi_mods_add(&smods, LDAP_MOD_ADD, "objectclass", strlen("ldapsubentry"),"ldapsubentry");
b045b9
-    slapi_mods_add(&smods, LDAP_MOD_REPLACE, "conflictcsn", strlen(csnstr),csn_as_string(opcsn, PR_FALSE, csnstr));
b045b9
+    slapi_mods_add(&smods, LDAP_MOD_REPLACE, "conflictcsn", CSN_STRSIZE, csn_as_string(opcsn, PR_FALSE, csnstr));
b045b9
     op_result = urp_fixup_modify_entry(uniqueid, entrysdn, opcsn, &smods, 0);
b045b9
     slapi_mods_done(&smods);
b045b9
     if (op_result == LDAP_TYPE_OR_VALUE_EXISTS) {
b045b9
diff --git a/ldap/servers/plugins/syntaxes/string.c b/ldap/servers/plugins/syntaxes/string.c
b045b9
index f50dc13..e05ca7f 100644
b045b9
--- a/ldap/servers/plugins/syntaxes/string.c
b045b9
+++ b/ldap/servers/plugins/syntaxes/string.c
b045b9
@@ -391,6 +391,7 @@ bailout:
b045b9
     if (free_re) {
b045b9
         slapi_re_free(re);
b045b9
     }
b045b9
+    slapi_ch_free_string(&alt;;
b045b9
     slapi_ch_free((void **)&tmpbuf); /* NULL is fine */
b045b9
     slapi_ch_free((void **)&bigpat); /* NULL is fine */
b045b9
 
b045b9
diff --git a/ldap/servers/slapd/back-ldbm/dbversion.c b/ldap/servers/slapd/back-ldbm/dbversion.c
b045b9
index 01f86f4..5a77abd 100644
b045b9
--- a/ldap/servers/slapd/back-ldbm/dbversion.c
b045b9
+++ b/ldap/servers/slapd/back-ldbm/dbversion.c
b045b9
@@ -159,7 +159,7 @@ dbversion_read(struct ldbminfo *li, const char *directory, char **ldbmversion, c
b045b9
         }
b045b9
         (void)PR_Close(prfd);
b045b9
 
b045b9
-        if (*dataversion == NULL) {
b045b9
+        if (dataversion == NULL || *dataversion == NULL) {
b045b9
             slapi_log_err(SLAPI_LOG_DEBUG, "dbversion_read", "dataversion not present in \"%s\"\n", filename);
b045b9
         }
b045b9
         if (*ldbmversion == NULL) {
b045b9
diff --git a/ldap/servers/slapd/back-ldbm/index.c b/ldap/servers/slapd/back-ldbm/index.c
b045b9
index 798480e..58b11ed 100644
b045b9
--- a/ldap/servers/slapd/back-ldbm/index.c
b045b9
+++ b/ldap/servers/slapd/back-ldbm/index.c
b045b9
@@ -1063,6 +1063,7 @@ index_read_ext_allids(
b045b9
             /* The database might not exist. We have to assume it means empty set */
b045b9
             slapi_log_err(SLAPI_LOG_TRACE, "index_read_ext_allids", "Failed to access idl index for %s\n", basetype);
b045b9
             slapi_log_err(SLAPI_LOG_TRACE, "index_read_ext_allids", "Assuming %s has no index values\n", basetype);
b045b9
+            idl_free(&idl);
b045b9
             idl = idl_alloc(0);
b045b9
             break;
b045b9
         } else {
b045b9
diff --git a/ldap/servers/slapd/conntable.c b/ldap/servers/slapd/conntable.c
b045b9
index c04ca0f..7c57b47 100644
b045b9
--- a/ldap/servers/slapd/conntable.c
b045b9
+++ b/ldap/servers/slapd/conntable.c
b045b9
@@ -347,7 +347,7 @@ connection_table_as_entry(Connection_Table *ct, Slapi_Entry *e)
b045b9
 
b045b9
         PR_EnterMonitor(ct->c[i].c_mutex);
b045b9
         if (ct->c[i].c_sd != SLAPD_INVALID_SOCKET) {
b045b9
-            char buf2[20];
b045b9
+            char buf2[SLAPI_TIMESTAMP_BUFSIZE+1];
b045b9
             size_t lendn = ct->c[i].c_dn ? strlen(ct->c[i].c_dn) : 6; /* "NULLDN" */
b045b9
             size_t lenip = ct->c[i].c_ipaddr ? strlen(ct->c[i].c_ipaddr) : 0;
b045b9
             size_t lenconn = 1;
b045b9
diff --git a/ldap/servers/slapd/modify.c b/ldap/servers/slapd/modify.c
b045b9
index 4b5a676..6309975 100644
b045b9
--- a/ldap/servers/slapd/modify.c
b045b9
+++ b/ldap/servers/slapd/modify.c
b045b9
@@ -923,6 +923,7 @@ op_shared_modify(Slapi_PBlock *pb, int pw_change, char *old_pw)
b045b9
             if (pw_encodevals_ext(pb, sdn, va)) {
b045b9
                 slapi_log_err(SLAPI_LOG_CRIT, "op_shared_modify", "Unable to hash userPassword attribute for %s.\n", slapi_entry_get_dn_const(e));
b045b9
                 send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL, "Unable to store attribute \"userPassword\" correctly\n", 0, NULL);
b045b9
+                valuearray_free(&va);
b045b9
                 goto free_and_return;
b045b9
             }
b045b9
 
b045b9
diff --git a/ldap/servers/slapd/plugin.c b/ldap/servers/slapd/plugin.c
b045b9
index f47ff9b..e02133a 100644
b045b9
--- a/ldap/servers/slapd/plugin.c
b045b9
+++ b/ldap/servers/slapd/plugin.c
b045b9
@@ -4242,7 +4242,7 @@ bail:
b045b9
 int
b045b9
 slapi_set_plugin_default_config(const char *type, Slapi_Value *value)
b045b9
 {
b045b9
-    Slapi_PBlock *pb = slapi_pblock_new();
b045b9
+    Slapi_PBlock *pb;
b045b9
     Slapi_Entry **entries = NULL;
b045b9
     int rc = LDAP_SUCCESS;
b045b9
     char **search_attrs = NULL; /* used by search */
b045b9
@@ -4251,6 +4251,7 @@ slapi_set_plugin_default_config(const char *type, Slapi_Value *value)
b045b9
         return rc;
b045b9
     }
b045b9
 
b045b9
+    pb = slapi_pblock_new();
b045b9
     charray_add(&search_attrs, slapi_ch_strdup(type));
b045b9
 
b045b9
     /* cn=plugin default config,cn=config */
b045b9
diff --git a/ldap/servers/slapd/referral.c b/ldap/servers/slapd/referral.c
b045b9
index c5d9ffc..5935820 100644
b045b9
--- a/ldap/servers/slapd/referral.c
b045b9
+++ b/ldap/servers/slapd/referral.c
b045b9
@@ -153,7 +153,7 @@ referrals_free(void)
b045b9
 struct berval **
b045b9
 ref_adjust(Slapi_PBlock *pb, struct berval **urls, const Slapi_DN *refsdn, int is_reference)
b045b9
 {
b045b9
-    int i, len, scope;
b045b9
+    int i, len, scope = 0;
b045b9
     Slapi_DN *sdn = NULL;
b045b9
     char *p, *opdn_norm;
b045b9
     struct berval **urlscopy;
b045b9
@@ -195,9 +195,9 @@ ref_adjust(Slapi_PBlock *pb, struct berval **urls, const Slapi_DN *refsdn, int i
b045b9
 
b045b9
     for (i = 0; urls[i] != NULL; ++i) {
b045b9
         /*
b045b9
-     * duplicate the URL, stripping off the label if there is one and
b045b9
-     * leaving extra room for "??base" in case we need to append that.
b045b9
-     */
b045b9
+         * duplicate the URL, stripping off the label if there is one and
b045b9
+         * leaving extra room for "??base" in case we need to append that.
b045b9
+         */
b045b9
         urlscopy[i] = (struct berval *)slapi_ch_malloc(
b045b9
             sizeof(struct berval));
b045b9
         if ((p = strchr(urls[i]->bv_val, ' ')) == NULL) {
b045b9
@@ -210,16 +210,16 @@ ref_adjust(Slapi_PBlock *pb, struct berval **urls, const Slapi_DN *refsdn, int i
b045b9
         urlscopy[i]->bv_val[len] = '\0';
b045b9
 
b045b9
         /*
b045b9
-     * adjust the baseDN as needed and set the length
b045b9
-     */
b045b9
+         * adjust the baseDN as needed and set the length
b045b9
+         */
b045b9
         adjust_referral_basedn(&urlscopy[i]->bv_val, refsdn,
b045b9
                                opdn_norm, is_reference);
b045b9
         urlscopy[i]->bv_len = strlen(urlscopy[i]->bv_val);
b045b9
 
b045b9
         /*
b045b9
-     * if we are dealing with a continuation reference that resulted
b045b9
-     * from a one-level search, add a scope of base to the URL.
b045b9
-     */
b045b9
+         * if we are dealing with a continuation reference that resulted
b045b9
+         * from a one-level search, add a scope of base to the URL.
b045b9
+         */
b045b9
         if (is_reference && operation_get_type(op) == SLAPI_OPERATION_SEARCH &&
b045b9
             scope == LDAP_SCOPE_ONELEVEL) {
b045b9
             strcat(urlscopy[i]->bv_val, "??base");
b045b9
diff --git a/ldap/servers/slapd/task.c b/ldap/servers/slapd/task.c
b045b9
index f3d02d9..53a0af5 100644
b045b9
--- a/ldap/servers/slapd/task.c
b045b9
+++ b/ldap/servers/slapd/task.c
b045b9
@@ -278,6 +278,10 @@ slapi_task_log_notice(Slapi_Task *task, char *format, ...)
b045b9
     char buffer[LOG_BUFFER];
b045b9
     size_t len;
b045b9
 
b045b9
+    if (task == NULL) {
b045b9
+        return;
b045b9
+    }
b045b9
+
b045b9
     va_start(ap, format);
b045b9
     PR_vsnprintf(buffer, LOG_BUFFER, format, ap);
b045b9
     va_end(ap);
b045b9
@@ -1089,11 +1093,11 @@ task_export_thread(void *arg)
b045b9
     slapi_pblock_get(pb, SLAPI_BACKEND_TASK, &task);
b045b9
 
b045b9
     g_incr_active_threadcnt();
b045b9
-    for (count = 0, inp = instance_names; *inp; inp++, count++)
b045b9
+    for (count = 0, inp = instance_names; inp && *inp; inp++, count++)
b045b9
         ;
b045b9
     slapi_task_begin(task, count);
b045b9
 
b045b9
-    for (inp = instance_names; *inp; inp++) {
b045b9
+    for (inp = instance_names; inp && *inp; inp++) {
b045b9
         int release_me = 0;
b045b9
         /* lookup the backend */
b045b9
         be = slapi_be_select_by_instance_name((const char *)*inp);
b045b9
diff --git a/ldap/servers/snmp/main.c b/ldap/servers/snmp/main.c
b045b9
index 8477831..5bd318d 100644
b045b9
--- a/ldap/servers/snmp/main.c
b045b9
+++ b/ldap/servers/snmp/main.c
b045b9
@@ -21,6 +21,7 @@
b045b9
 #include "ldap.h"
b045b9
 #include "ldif.h"
b045b9
 #include <ctype.h>
b045b9
+#include <errno.h>
b045b9
 
b045b9
 static char *agentx_master = NULL;
b045b9
 static char *agent_logdir = NULL;
b045b9
@@ -54,9 +55,17 @@ main(int argc, char *argv[])
b045b9
     {
b045b9
         char *s = getenv("DEBUG_SLEEP");
b045b9
         if ((s != NULL) && isdigit(*s)) {
b045b9
-            int secs = atoi(s);
b045b9
+            char *endp = NULL;
b045b9
+            long secs;
b045b9
+            errno = 0;
b045b9
+
b045b9
             printf("%s pid is %d\n", argv[0], getpid());
b045b9
-            sleep(secs);
b045b9
+            secs = strtol(s, &endp, 10);
b045b9
+            if (*endp != '\0' || errno == ERANGE) {
b045b9
+                sleep(10);
b045b9
+            } else {
b045b9
+                sleep(secs);
b045b9
+            }
b045b9
         }
b045b9
     }
b045b9
 
b045b9
-- 
b045b9
2.9.5
b045b9