Selaa lähdekoodia

Resolves: #196523
Summary: miscellaneous memory leaks
Description: 1) fixed memory leaks
2) cleaned up normalize_path code with fixing memory leaks

Noriko Hosoi 18 vuotta sitten
vanhempi
sitoutus
464b322594

+ 12 - 11
ldap/servers/plugins/replication/repl5_protocol_util.c

@@ -117,6 +117,10 @@ acquire_replica(Private_Repl_Protocol *prp, char *prot_oid, RUV **ruv)
 	int return_value;
 	ConnResult crc;
 	Repl_Connection *conn;
+	struct berval *retdata = NULL;
+	char *retoid = NULL;
+	Slapi_DN *replarea_sdn = NULL;
+	struct berval **ruv_bervals = NULL;
 
 	PR_ASSERT(prp && prot_oid);
 
@@ -200,9 +204,6 @@ acquire_replica(Private_Repl_Protocol *prp, char *prot_oid, RUV **ruv)
 			} else 
 			{
 				CSN *current_csn = NULL;
-				struct berval *retdata = NULL;
-				char *retoid = NULL;
-				Slapi_DN *replarea_sdn;
 
 				/* Good to go. Start the protocol. */
 
@@ -243,7 +244,6 @@ acquire_replica(Private_Repl_Protocol *prp, char *prot_oid, RUV **ruv)
 						 * Extop was processed. Look at extop response to see if we're
 						 * permitted to go ahead.
 						 */
-						struct berval **ruv_bervals = NULL;
 						int extop_result;
 						int extop_rc = decode_repl_ext_response(retdata, &extop_result,
 																&ruv_bervals);
@@ -411,8 +411,6 @@ acquire_replica(Private_Repl_Protocol *prp, char *prot_oid, RUV **ruv)
 							prp->last_acquire_response_code = NSDS50_REPL_INTERNAL_ERROR;
 							return_value = ACQUIRE_FATAL_ERROR;
 						}
-						if (NULL != ruv_bervals)
-							ber_bvecfree(ruv_bervals);
 					}
 					else
 					{
@@ -437,15 +435,18 @@ acquire_replica(Private_Repl_Protocol *prp, char *prot_oid, RUV **ruv)
 						agmt_get_long_name(prp->agmt));
 					return_value = ACQUIRE_FATAL_ERROR;
 				}
-				slapi_sdn_free(&replarea_sdn);
-				if (NULL != retoid)
-					ldap_memfree(retoid);
-				if (NULL != retdata)
-					ber_bvfree(retdata);
 			}
 		}
 	}
 error:
+	if (NULL != ruv_bervals)
+		ber_bvecfree(ruv_bervals);
+	if (NULL != replarea_sdn)
+		slapi_sdn_free(&replarea_sdn);
+	if (NULL != retoid)
+		ldap_memfree(retoid);
+	if (NULL != retdata)
+		ber_bvfree(retdata);
 
 	if (ACQUIRE_SUCCESS != return_value)
 	{

+ 3 - 0
ldap/servers/slapd/sasl_io.c

@@ -139,6 +139,7 @@ int
 sasl_io_cleanup(Connection *c)
 {
     int ret = 0;
+    struct lber_x_ext_io_fns *func_pointers = NULL;
     sasl_io_private *sp = c->c_sasl_io_private;
     if (sp) {
         LDAPDebug( LDAP_DEBUG_CONNS,
@@ -147,6 +148,8 @@ sasl_io_cleanup(Connection *c)
         slapi_ch_free((void**)&(sp->encrypted_buffer));
         slapi_ch_free((void**)&(sp->decrypted_buffer));
         /* Put the I/O functions back how they were */
+        ber_sockbuf_get_option( c->c_sb, LBER_SOCKBUF_OPT_EXT_IO_FNS, &func_pointers);
+        slapi_ch_free((void**)&func_pointers);
         ber_sockbuf_set_option( c->c_sb, LBER_SOCKBUF_OPT_EXT_IO_FNS, sp->real_iofns);
         slapi_ch_free((void**)&sp);
         c->c_sasl_io_private = NULL;

+ 32 - 30
ldap/servers/slapd/util.c

@@ -402,61 +402,63 @@ is_abspath(const char *path)
 static void
 clean_path(char **norm_path)
 {
-	char **np;
+    char **np;
 
-	for (np = norm_path; np && *np; np++)
-		slapi_ch_free((void **)np);
-	slapi_ch_free((void  **)&norm_path);
+    for (np = norm_path; np && *np; np++)
+        slapi_ch_free_string(np);
+    slapi_ch_free((void  **)&norm_path);
 }
 
 static char **
 normalize_path(char *path)
 {
-    char *dname = slapi_ch_strdup(path);
-    char *dnamep = dname;
-    char *bnamep = NULL;
+    char *dname = NULL;
+    char *dnamep = NULL;
     char **dirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *));
     char **rdirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *));
     char **dp = dirs;
     char **rdp;
+    int elimdots = 0;
+
+    if (NULL == path || '\0' == *path) {
+        return NULL;
+    }
+
+    dname = slapi_ch_strdup(path);
     do {
-        bnamep = strrchr(dnamep, _CSEP);
-        if (NULL == bnamep) {
-            bnamep = dnamep;
+        dnamep = strrchr(dname, _CSEP);
+        if (NULL == dnamep) {
+            dnamep = dname;
         } else {
-            *bnamep = '\0';
-            bnamep++;
+            *dnamep = '\0';
+            dnamep++;
         }
-        if (0 != strcmp(bnamep, ".")) {
-            *dp++ = slapi_ch_strdup(bnamep);    /* remove "/./" in the path */
+        if (0 != strcmp(dnamep, ".") && strlen(dnamep) > 0) {
+            *dp++ = slapi_ch_strdup(dnamep); /* rm "/./" and "//" in the path */
         }
-    } while (NULL != dnamep && '\0' != *dnamep && /* done or relative path */
-             !(0 == strcmp(dnamep, ".") && 0 == strcmp(bnamep, ".")));
+    } while ( dnamep > dname /* == -> no more _CSEP */ );
+    slapi_ch_free_string(&dname);
 
     /* remove "xxx/.." in the path */
     for (dp = dirs, rdp = rdirs; dp && *dp; dp++) {
         while (*dp && 0 == strcmp(*dp, "..")) {
             dp++; 
-            if (rdp > rdirs)
-                rdp--;
+            elimdots++;
         }
-        if (*dp)
+        if (elimdots > 0) {
+            elimdots--;
+        } else if (*dp) {
             *rdp++ = slapi_ch_strdup(*dp);
-    }
-    for (--dp, rdp = rdirs; dp >= dirs; dp--) {
-        while (*dp && 0 == strcmp(*dp, "..")) {
-            dp--; 
-            if (rdp > rdirs)
-                rdp--;
         }
-        if (*dp && strlen(*dp) > 0)
-            *rdp++ = slapi_ch_strdup(*dp);
     }
-    *rdp = NULL;
+    /* reverse */
+    for (--rdp, dp = rdirs; rdp >= dp && rdp >= rdirs; --rdp, dp++) {
+        char *tmpp = *dp;
+        *dp = *rdp;
+        *rdp = tmpp;
+    }
 
     clean_path(dirs);
-    slapi_ch_free_string(&dname);
-
     return rdirs;
 }