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

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