From 88810029a49f1587bbd0700254a97c943d631b4c Mon Sep 17 00:00:00 2001
From: Noriko Hosoi <nhosoi@redhat.com>
Date: Tue, 23 Jun 2015 12:46:46 -0700
Subject: [PATCH] Ticket #48203 - Fix coverity issues - 06/22/2015

Description:
13294	Uninitialized scalar variable -- retrocl_init_trimming (introduced by #47669)
13293	Resource leak -- retrocl_init_trimming (introduced by #47669)

2. Defect type: CHECKED_RETURN
   50. ldap/servers/slapd/tools/ldclt/ldapfct.c:1945:
   9.  ldap/servers/slapd/tools/ldclt/ldapfct.c:952:
       check_return: Calling "addErrorStat" without checking return value
       (as is done elsewhere 26 out of 28 times).

1. Defect type: COMPILER_WARNING
   2.  ldap/servers/slapd/daemon.c:1412:21:
       warning: 'tp' may be used uninitialized in this function [-Wmaybe-uninitialized]
---
 ldap/servers/plugins/retrocl/retrocl_trim.c |   3 +-
 ldap/servers/slapd/daemon.c                 |   2 +-
 ldap/servers/slapd/tools/ldclt/ldapfct.c    | 186 ++++++++++++++--------------
 3 files changed, 93 insertions(+), 98 deletions(-)

diff --git a/ldap/servers/plugins/retrocl/retrocl_trim.c b/ldap/servers/plugins/retrocl/retrocl_trim.c
index 65f3015..b09f669 100644
--- a/ldap/servers/plugins/retrocl/retrocl_trim.c
+++ b/ldap/servers/plugins/retrocl/retrocl_trim.c
@@ -412,7 +412,7 @@ void retrocl_housekeeping ( time_t cur_time, void *noarg )
 void retrocl_init_trimming (void)
 {
     const char *cl_maxage;
-    time_t ageval;
+    time_t ageval = 0; /* Don't trim, by default */
     const char *cl_trim_interval;
     
     cl_maxage = retrocl_get_config_str(CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE);
@@ -425,6 +425,7 @@ void retrocl_init_trimming (void)
                         "retrocl_init_trimming: ignoring invalid %s value %s; "
                         "not trimming retro changelog.\n",
                         CONFIG_CHANGELOG_MAXAGE_ATTRIBUTE, cl_maxage);
+            slapi_ch_free_string((char **)&cl_maxage);
             return;
         }
     }
diff --git a/ldap/servers/slapd/daemon.c b/ldap/servers/slapd/daemon.c
index 436f3d5..ba73da3 100644
--- a/ldap/servers/slapd/daemon.c
+++ b/ldap/servers/slapd/daemon.c
@@ -1026,7 +1026,7 @@ void slapd_daemon( daemon_ports_t *ports )
 	int threads;
 	int in_referral_mode = config_check_referral_mode();
 #ifdef ENABLE_NUNC_STANS
-	ns_thrpool_t *tp;
+	ns_thrpool_t *tp = NULL;
 	struct ns_thrpool_config tp_config;
 #endif
 	int connection_table_size = get_configured_connection_table_size();
diff --git a/ldap/servers/slapd/tools/ldclt/ldapfct.c b/ldap/servers/slapd/tools/ldclt/ldapfct.c
index bc8c89d..5001e3a 100644
--- a/ldap/servers/slapd/tools/ldclt/ldapfct.c
+++ b/ldap/servers/slapd/tools/ldclt/ldapfct.c
@@ -166,8 +166,7 @@ getBindAndPasswdFromFile (
    * Skip the '\t' to find the password.
    * The password is from this place up to the end of the line.
    */
-  while (mctx.rndBindDlf->str[num][i]!='\0' && 
-	 mctx.rndBindDlf->str[num][i]=='\t')
+  while (mctx.rndBindDlf->str[num][i]!='\0' && mctx.rndBindDlf->str[num][i]=='\t')
     i++;
   if (mctx.rndBindDlf->str[num][i] == '\0')
   {
@@ -675,54 +674,50 @@ connectToLDAP(thread_context *tttctx, const char *bufBindDN, const char *bufPass
   if (tttctx) {
     binded = tttctx->binded;
   }
-  if ((mode & CLTAUTH) && ((!(binded)) ||
-			   (mode & BIND_EACH_OPER))) {
+  if ((mode & CLTAUTH) && ((!(binded)) || (mode & BIND_EACH_OPER))) {
     if (mode & VERY_VERBOSE)
-      printf ("ldclt[%d]: T%03d: Before ldap_sasl_bind_s\n",
-	      mctx.pid, thrdNum);
-    ret = ldap_sasl_bind_s (ld, "", "EXTERNAL", NULL, NULL, NULL,
-			    NULL);
+      printf ("ldclt[%d]: T%03d: Before ldap_sasl_bind_s\n", mctx.pid, thrdNum);
+    ret = ldap_sasl_bind_s (ld, "", "EXTERNAL", NULL, NULL, NULL, NULL);
     if (mode & VERY_VERBOSE)
-      printf ("ldclt[%d]: T%03d: After ldap_sasl_bind_s\n",
-	      mctx.pid, thrdNum);
-    if (ret == LDAP_SUCCESS) {				/*JLS 18-12-00*/
+      printf ("ldclt[%d]: T%03d: After ldap_sasl_bind_s\n", mctx.pid, thrdNum);
+    if (ret == LDAP_SUCCESS) {                                /*JLS 18-12-00*/
       if (tttctx) {
-	tttctx->binded = 1;					/*JLS 18-12-00*/
+        tttctx->binded = 1;                                        /*JLS 18-12-00*/
       }
-    } else {						/*JLS 18-12-00*/
+    } else {                                                /*JLS 18-12-00*/
       if (tttctx) {
-	tttctx->binded = 0;					/*JLS 18-12-00*/
+        tttctx->binded = 0;                                        /*JLS 18-12-00*/
       }
-      if (ignoreError (ret)) {				/*JLS 18-12-00*/
-	if (!(mode & QUIET)) {			/*JLS 18-12-00*/
-	  printf ("ldclt[%d]: T%03d: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
-		  mctx.pid, thrdNum, ret, my_ldap_err2string (ret));
-	  fflush (stdout);					/*JLS 18-12-00*/
-	}							/*JLS 18-12-00*/
-	if (addErrorStat (ret) < 0)				/*JLS 18-12-00*/
-	  ret = -1;
-	else
-	  ret = 0;
-	goto done;
-      } else {						/*JLS 18-12-00*/
-	printf ("ldclt[%d]: T%03d: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
-		mctx.pid, thrdNum, ret, my_ldap_err2string (ret));
-	fflush (stdout);					/*JLS 18-12-00*/
-	if (tttctx)
-	  tttctx->exitStatus = EXIT_NOBIND;			/*JLS 18-12-00*/
-	(void)addErrorStat(ret);
-	ret = -1;
-	goto done;
-      }								/*JLS 18-12-00*/
+      if (ignoreError (ret)) {                                /*JLS 18-12-00*/
+        if (!(mode & QUIET)) {                        /*JLS 18-12-00*/
+          printf ("ldclt[%d]: T%03d: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
+                  mctx.pid, thrdNum, ret, my_ldap_err2string (ret));
+          fflush (stdout);                                        /*JLS 18-12-00*/
+        }                                                        /*JLS 18-12-00*/
+        if (addErrorStat (ret) < 0)                                /*JLS 18-12-00*/
+          ret = -1;
+        else
+          ret = 0;
+        goto done;
+      } else {                                                /*JLS 18-12-00*/
+        printf ("ldclt[%d]: T%03d: Cannot ldap_sasl_bind_s, error=%d (%s)\n",
+                mctx.pid, thrdNum, ret, my_ldap_err2string (ret));
+        fflush (stdout);                                        /*JLS 18-12-00*/
+        if (tttctx)
+          tttctx->exitStatus = EXIT_NOBIND;                        /*JLS 18-12-00*/
+        (void)addErrorStat(ret);
+        ret = -1;
+        goto done;
+      }                                                                /*JLS 18-12-00*/
     }
   } else if ((mod2 & M2_SASLAUTH) && ((!(binded)) ||
-				      (mode & BIND_EACH_OPER))) {
+                                      (mode & BIND_EACH_OPER))) {
     void *defaults;
     char *my_saslauthid = NULL;
 
     if ( mctx.sasl_mech == NULL) {
       fprintf( stderr, "Please specify the SASL mechanism name when "
-	       "using SASL options\n");
+               "using SASL options\n");
       ret = -1;
       goto done;
     }
@@ -732,8 +727,7 @@ connectToLDAP(thread_context *tttctx, const char *bufBindDN, const char *bufPass
                              (void *) mctx.sasl_secprops );
 
       if ( ret != LDAP_SUCCESS ) {
-        fprintf( stderr, "Unable to set LDAP_OPT_X_SASL_SECPROPS: %s\n",
-		 mctx.sasl_secprops );
+        fprintf( stderr, "Unable to set LDAP_OPT_X_SASL_SECPROPS: %s\n", mctx.sasl_secprops );
         goto done;
       }
     }
@@ -949,7 +943,7 @@ connectToServer (
       fprintf (stderr, "ldclt[%d]: T%03d: cannot ldap_unbind(), error=%d (%s)\n",
 			mctx.pid, tttctx->thrdNum, ret,strerror (ret));
       fflush (stderr);
-      addErrorStat(ret);
+      (void)addErrorStat(ret);
       return (-1);
     }
     tttctx->ldapCtx = NULL;
@@ -1942,7 +1936,7 @@ createMissingNodes (
     printf ("ldclt[%d]: T%03d: Cannot add (%s), error=%d (%s)\n",
 	mctx.pid, tttctx->thrdNum, nodeDN, ret, my_ldap_err2string (ret));
     fflush (stdout);
-    addErrorStat(ret);
+    (void)addErrorStat(ret);
     return (-1);
   }
 
@@ -2907,17 +2901,17 @@ doAttrReplace (
     {
       if (!((mctx.mode & QUIET) && ignoreError (ret)))
       {
-	printf ("ldclt[%d]: T%03d: Cannot modify (%s), error=%d (%s)\n",
-		mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
-	fflush (stdout);
+        printf("ldclt[%d]: T%03d: Cannot modify (%s), error=%d (%s)\n",
+               mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
+        fflush (stdout);
       }
       if (addErrorStat (ret) < 0)
-	return (-1);
+        return (-1);
     }
     else
     {
       if (incrementNbOpers (tttctx) < 0)	/* Memorize operation */
-	return (-1);
+        return (-1);
     }
 
     /*
@@ -3062,19 +3056,19 @@ doAttrFileReplace (
     {
       if (!((mctx.mode & QUIET) && ignoreError (ret)))
       {
-	printf ("ldclt[%d]: T%03d: AttriFileReplace Error Cannot modify (%s), error=%d (%s)\n",
-		mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
-	fflush (stdout);
+        printf ("ldclt[%d]: T%03d: AttriFileReplace Error Cannot modify (%s), error=%d (%s)\n",
+                mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
+        fflush (stdout);
       }
       if (addErrorStat (ret) < 0)
-	return (-1);
+        return (-1);
     }
     else
     {
       printf ("ldclt[%d]: T%03d: AttriFileReplace modify (%s) success ,\n",
-		mctx.pid, tttctx->thrdNum, newDn);
-      if (incrementNbOpers (tttctx) < 0)	/* Memorize operation */
-	return (-1);
+                mctx.pid, tttctx->thrdNum, newDn);
+      if (incrementNbOpers (tttctx) < 0)        /* Memorize operation */
+        return (-1);
     }
 
     /*
@@ -3138,12 +3132,12 @@ doAttrFileReplace (
     {
       if (!((mctx.mode & QUIET) && ignoreError (ret)))
       {
-	printf ("ldclt[%d]: T%03d: Cannot modify(%s), error=%d (%s)\n",
-		mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
-	fflush (stdout);
+        printf ("ldclt[%d]: T%03d: Cannot modify(%s), error=%d (%s)\n",
+                mctx.pid, tttctx->thrdNum, newDn, ret, my_ldap_err2string (ret));
+        fflush (stdout);
       }
       if (addErrorStat (ret) < 0)
-	return (-1);
+        return (-1);
     }
     else
     {
@@ -3151,9 +3145,9 @@ doAttrFileReplace (
        * Memorize the operation
        */
       if (msgIdAdd (tttctx, msgid, newDn, newDn, attrs) < 0)
-	return (-1);
+        return (-1);
       if (incrementNbOpers (tttctx) < 0)
-	return (-1);
+        return (-1);
       tttctx->pendingNb++;
     }
   }
@@ -3216,28 +3210,28 @@ doDeleteEntry (
     {
       if (!((mctx.mode & QUIET) && ignoreError (ret)))
       {
-	printf ("ldclt[%d]: T%03d: Cannot ldap_delete (%s), error=%d (%s)\n",
-		mctx.pid, tttctx->thrdNum, delDn, ret, my_ldap_err2string (ret));
-	fflush (stdout);
+        printf ("ldclt[%d]: T%03d: Cannot ldap_delete (%s), error=%d (%s)\n",
+                mctx.pid, tttctx->thrdNum, delDn, ret, my_ldap_err2string (ret));
+        fflush (stdout);
       }
       if (addErrorStat (ret) < 0)
-	return (-1);
-      if ((ret == LDAP_NO_SUCH_OBJECT) && 			/*JLS 15-12-00*/
-	  (mctx.mode & COUNT_EACH))				/*JLS 15-12-00*/
-      {								/*JLS 15-12-00*/
-	if (incrementNbOpers (tttctx) < 0)			/*JLS 15-12-00*/
-	  return (-1);						/*JLS 15-12-00*/
-      }								/*JLS 15-12-00*/
+        return (-1);
+      if ((ret == LDAP_NO_SUCH_OBJECT) &&                         /*JLS 15-12-00*/
+          (mctx.mode & COUNT_EACH))                                /*JLS 15-12-00*/
+      {                                                                /*JLS 15-12-00*/
+        if (incrementNbOpers (tttctx) < 0)                        /*JLS 15-12-00*/
+          return (-1);                                                /*JLS 15-12-00*/
+      }                                                                /*JLS 15-12-00*/
     }
     else
     {
-      if (incrementNbOpers (tttctx) < 0)	/* Memorize operation */
-	return (-1);
-#ifdef SOLARIS							/*JLS 14-11-00*/
+      if (incrementNbOpers (tttctx) < 0)        /* Memorize operation */
+        return (-1);
+#ifdef SOLARIS                                                        /*JLS 14-11-00*/
       if (mctx.slavesNb > 0)
-	if (opAdd (tttctx, LDAP_REQ_DELETE, delDn, NULL, NULL, NULL) < 0)
-	  return (-1);
-#endif								/*JLS 14-11-00*/
+        if (opAdd (tttctx, LDAP_REQ_DELETE, delDn, NULL, NULL, NULL) < 0)
+          return (-1);
+#endif                                                                /*JLS 14-11-00*/
     }
 
     /*
@@ -3277,8 +3271,8 @@ doDeleteEntry (
     int msgid = 0;
 
     if ((mctx.mode & VERBOSE)   &&
-	(tttctx->asyncHit == 1) &&
-	(!(mctx.mode & SUPER_QUIET)))
+        (tttctx->asyncHit == 1) &&
+        (!(mctx.mode & SUPER_QUIET)))
     {
       tttctx->asyncHit = 0;
       printf ("ldclt[%d]: T%03d: Restart sending.\n",
@@ -3300,27 +3294,27 @@ doDeleteEntry (
     {
       if (ldap_get_option (tttctx->ldapCtx, LDAP_OPT_ERROR_NUMBER, &ret) < 0)
       {
-	printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
-		mctx.pid, tttctx->thrdNum);
-	fflush (stdout);
-	return (-1);
+        printf ("ldclt[%d]: T%03d: Cannot ldap_get_option(LDAP_OPT_ERROR_NUMBER)\n",
+                mctx.pid, tttctx->thrdNum);
+        fflush (stdout);
+        return (-1);
       }
       else
       {
-	if (!((mctx.mode & QUIET) && ignoreError (ret)))
-	{
-	  printf ("ldclt[%d]: T%03d: Cannot ldap_delete(), error=%d (%s)\n",
-		mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
-	  fflush (stdout);
-	}
-	if (addErrorStat (ret) < 0)
-	  return (-1);
-	if ((ret == LDAP_NO_SUCH_OBJECT) && 			/*JLS 15-12-00*/
-	    (mctx.mode & COUNT_EACH))				/*JLS 15-12-00*/
-	{							/*JLS 15-12-00*/
-	  if (incrementNbOpers (tttctx) < 0)			/*JLS 15-12-00*/
-	    return (-1);					/*JLS 15-12-00*/
-	}							/*JLS 15-12-00*/
+        if (!((mctx.mode & QUIET) && ignoreError (ret)))
+        {
+          printf ("ldclt[%d]: T%03d: Cannot ldap_delete(), error=%d (%s)\n",
+                mctx.pid, tttctx->thrdNum, ret, my_ldap_err2string (ret));
+          fflush (stdout);
+        }
+        if (addErrorStat (ret) < 0)
+          return (-1);
+        if ((ret == LDAP_NO_SUCH_OBJECT) &&                         /*JLS 15-12-00*/
+            (mctx.mode & COUNT_EACH))                                /*JLS 15-12-00*/
+        {                                                        /*JLS 15-12-00*/
+          if (incrementNbOpers (tttctx) < 0)                        /*JLS 15-12-00*/
+            return (-1);                                        /*JLS 15-12-00*/
+        }                                                        /*JLS 15-12-00*/
       }
     }
     else
@@ -3329,7 +3323,7 @@ doDeleteEntry (
        * Memorize the operation
        */
       if (incrementNbOpers (tttctx) < 0)
-	return (-1);
+        return (-1);
       tttctx->pendingNb++;
     }
   }
-- 
1.9.3

