From dcae9b06ab52169667c54e5357d02028e34efc55 Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Tue, 7 Mar 2017 15:26:39 -0500
Subject: [PATCH] Issue 49158 - fix latest coverity issues

14094 - dereference after null check in cl_cache.c
13685 - dereference before null check in cb_search.c
13680 - Explicit null dereferenced in memberof.c
13682 - Bad use of null-like value in ticket48005_test.py

https://pagure.io/389-ds-base/issue/49158

Reviewed by: ?
---
 dirsrvtests/tests/tickets/ticket48005_test.py  |  2 +-
 ldap/servers/plugins/chainingdb/cb_search.c    | 13 ++++---------
 ldap/servers/plugins/memberof/memberof.c       | 14 +++++++-------
 ldap/servers/plugins/replication/cl5_clcache.c |  2 +-
 4 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/dirsrvtests/tests/tickets/ticket48005_test.py b/dirsrvtests/tests/tickets/ticket48005_test.py
index cd3aa20..2c29be6 100644
--- a/dirsrvtests/tests/tickets/ticket48005_test.py
+++ b/dirsrvtests/tests/tickets/ticket48005_test.py
@@ -54,7 +54,7 @@ def test_ticket48005_setup(topology_st):
     if hasattr(topology_st.standalone, 'prefix'):
         prefix = topology_st.standalone.prefix
     else:
-        prefix = None
+        prefix = ""
     dbgen_prog = prefix + '/bin/dbgen.pl'
     log.info('dbgen_prog: %s' % dbgen_prog)
     os.system('%s -s %s -o %s -u -n 10000' % (dbgen_prog, SUFFIX, ldif_file))
diff --git a/ldap/servers/plugins/chainingdb/cb_search.c b/ldap/servers/plugins/chainingdb/cb_search.c
index 1059feb..2cd8221 100644
--- a/ldap/servers/plugins/chainingdb/cb_search.c
+++ b/ldap/servers/plugins/chainingdb/cb_search.c
@@ -626,11 +626,9 @@ chainingdb_next_search_entry ( Slapi_PBlock *pb )
 			}
 
 			/*
-			** build a dummy entry on the fly with a ref attribute
-			*/
-
+			 * build a dummy entry on the fly with a ref attribute
+			 */
 			{
-
 				struct  berval          bv;
 				int 			i;
 				struct  berval          *bvals[2];
@@ -644,7 +642,7 @@ chainingdb_next_search_entry ( Slapi_PBlock *pb )
 				bv.bv_len=strlen(bv.bv_val);
 				slapi_entry_add_values( anEntry, "objectclass", bvals);
 
-				for (i=0;referrals[i] != NULL; i++) {
+				for (i=0; referrals && referrals[i] != NULL; i++) {
 					bv.bv_val=referrals[i];
 					bv.bv_len=strlen(bv.bv_val);
 					slapi_entry_add_values( anEntry, "ref", bvals);
@@ -654,10 +652,7 @@ chainingdb_next_search_entry ( Slapi_PBlock *pb )
 				slapi_pblock_set( pb, SLAPI_SEARCH_RESULT_ENTRY,anEntry);
 				cb_set_acl_policy(pb);
 			}
-
-			if (referrals != NULL) {
-				   slapi_ldap_value_free( referrals );
-			}
+			slapi_ldap_value_free( referrals );
 			
 			return 0;
 
diff --git a/ldap/servers/plugins/memberof/memberof.c b/ldap/servers/plugins/memberof/memberof.c
index 7e4b828..81ef092 100644
--- a/ldap/servers/plugins/memberof/memberof.c
+++ b/ldap/servers/plugins/memberof/memberof.c
@@ -605,7 +605,7 @@ int memberof_postop_del(Slapi_PBlock *pb)
 			Slapi_Attr *attr = 0;
 
 			/* Loop through to find each grouping attribute separately. */
-			for (i = 0; configCopy.groupattrs[i] && ret == LDAP_SUCCESS; i++)
+			for (i = 0; configCopy.groupattrs && configCopy.groupattrs[i] && ret == LDAP_SUCCESS; i++)
 			{
 				if (0 == slapi_entry_attr_find(e, configCopy.groupattrs[i], &attr))
 				{
@@ -995,7 +995,7 @@ int memberof_postop_modrdn(Slapi_PBlock *pb)
 
 			/* get a list of member attributes present in the group
 			 * entry that is being renamed. */
-			for (i = 0; configCopy.groupattrs[i]; i++)
+			for (i = 0; configCopy.groupattrs && configCopy.groupattrs[i]; i++)
 			{
 				if(0 == slapi_entry_attr_find(post_e, configCopy.groupattrs[i], &attr))
 				{
@@ -1029,7 +1029,7 @@ int memberof_postop_modrdn(Slapi_PBlock *pb)
 					Slapi_Attr *attr = 0;
 
 					/* Loop through to find each grouping attribute separately. */
-					for (i = 0; configCopy.groupattrs[i] && ret == LDAP_SUCCESS; i++) {
+					for (i = 0; configCopy.groupattrs && configCopy.groupattrs[i] && ret == LDAP_SUCCESS; i++) {
 						if (0 == slapi_entry_attr_find(pre_e, configCopy.groupattrs[i], &attr)) {
 							if((ret = memberof_del_attr_list(pb, &configCopy, pre_sdn, attr))){
 								slapi_log_err(SLAPI_LOG_ERR, MEMBEROF_PLUGIN_SUBSYSTEM,
@@ -1779,7 +1779,7 @@ memberof_modop_one_replace_r(Slapi_PBlock *pb, MemberOfConfig *config,
 		ll->next = stack;
 		
 		/* Go through each grouping attribute one at a time. */
-		for (i = 0; config->groupattrs[i]; i++)
+		for (i = 0; config->groupattrs && config->groupattrs[i]; i++)
 		{
 			slapi_entry_attr_find( e, config->groupattrs[i], &members );
 			if(members)
@@ -2513,7 +2513,7 @@ int memberof_is_direct_member(MemberOfConfig *config, Slapi_Value *groupdn,
 	if(group_e)
 	{
 		/* See if memberdn is referred to by any of the group attributes. */
-		for (i = 0; config->groupattrs[i]; i++)
+		for (i = 0; config->groupattrs && config->groupattrs[i]; i++)
 		{
 			slapi_entry_attr_find(group_e, config->groupattrs[i], &attr );
 			if(attr && (0 == slapi_attr_value_find(attr, slapi_value_get_berval(memberdn))))
@@ -2542,7 +2542,7 @@ static int memberof_is_grouping_attr(char *type, MemberOfConfig *config)
 	int match = 0;
 	int i = 0;
 
-	for (i = 0; config && config->groupattrs[i]; i++)
+	for (i = 0; config && config->groupattrs && config->groupattrs[i]; i++)
 	{
 		match = slapi_attr_types_equivalent(type, config->groupattrs[i]);
 		if (match)
@@ -2768,7 +2768,7 @@ memberof_replace_list(Slapi_PBlock *pb, MemberOfConfig *config,
 	slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &pre_e );
 	slapi_pblock_get( pb, SLAPI_ENTRY_POST_OP, &post_e );
 		
-	for (i = 0; config && config->groupattrs[i]; i++)
+	for (i = 0; config && config->groupattrs && config->groupattrs[i]; i++)
 	{
 		if(pre_e && post_e)
 		{
diff --git a/ldap/servers/plugins/replication/cl5_clcache.c b/ldap/servers/plugins/replication/cl5_clcache.c
index bf8c471..5dda75d 100644
--- a/ldap/servers/plugins/replication/cl5_clcache.c
+++ b/ldap/servers/plugins/replication/cl5_clcache.c
@@ -402,7 +402,7 @@ clcache_load_buffer_bulk ( CLC_Buffer *buf, int flag )
 #endif
 
 	if (NULL == buf) {
-		slapi_log_err(SLAPI_LOG_ERR, buf->buf_agmt_name, 
+		slapi_log_err(SLAPI_LOG_ERR, get_thread_private_agmtname(),
 				"clcache_load_buffer_bulk - NULL buf\n" );
 		return rc;
 	}
-- 
2.7.4

