From 8e60c8e287ba54ceee765cea1f5abb5f90a5f300 Mon Sep 17 00:00:00 2001
From: Kurt Zeilenga <kurt@openldap.org>
Date: Tue, 1 Jun 1999 19:08:27 +0000
Subject: [PATCH] Clean up memory handlers.  Share vector free routines...
 ber_memvfree().

---
 libraries/libldap/apitest.c   |  2 +-
 libraries/libldap/free.c      |  6 +++
 libraries/libldap/getvalues.c | 18 +-------
 libraries/libldap/ldap-int.h  | 20 ++++----
 libraries/libldap/schema.c    | 86 ++++++++++++++---------------------
 5 files changed, 55 insertions(+), 77 deletions(-)

diff --git a/libraries/libldap/apitest.c b/libraries/libldap/apitest.c
index e27028a8d6..cbab301c27 100644
--- a/libraries/libldap/apitest.c
+++ b/libraries/libldap/apitest.c
@@ -126,8 +126,8 @@ main(int argc, char **argv)
 #else
 			printf("                     %s\n",
 				api.ldapai_extensions[i]);
-
 #endif
+
 			ldap_memfree(api.ldapai_extensions[i]);
 		}
 		ldap_memfree(api.ldapai_extensions);
diff --git a/libraries/libldap/free.c b/libraries/libldap/free.c
index f55488ea5f..2d5b390a16 100644
--- a/libraries/libldap/free.c
+++ b/libraries/libldap/free.c
@@ -30,6 +30,12 @@ ldap_memfree( void *p )
 	LDAP_FREE( p );
 }
 
+void
+ldap_memvfree( void **v )
+{
+	LDAP_VFREE( v );
+}
+
 void *
 ldap_memalloc( size_t s )
 {
diff --git a/libraries/libldap/getvalues.c b/libraries/libldap/getvalues.c
index 8f352dcd61..d886ab0689 100644
--- a/libraries/libldap/getvalues.c
+++ b/libraries/libldap/getvalues.c
@@ -147,25 +147,11 @@ ldap_count_values_len( struct berval **vals )
 void
 ldap_value_free( char **vals )
 {
-	int	i;
-
-	if ( vals == NULL )
-		return;
-	for ( i = 0; vals[i] != NULL; i++ )
-		LDAP_FREE( vals[i] );
-	LDAP_FREE( (char *) vals );
+	LDAP_VFREE( vals );
 }
 
 void
 ldap_value_free_len( struct berval **vals )
 {
-	int	i;
-
-	if ( vals == NULL )
-		return;
-	for ( i = 0; vals[i] != NULL; i++ ) {
-		LDAP_FREE( vals[i]->bv_val );
-		LDAP_FREE( vals[i] );
-	}
-	LDAP_FREE( (char *) vals );
+	ber_bvecfree( vals );
 }
diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h
index 684f3dc024..ff2f2da311 100644
--- a/libraries/libldap/ldap-int.h
+++ b/libraries/libldap/ldap-int.h
@@ -32,23 +32,25 @@
 LDAP_BEGIN_DECL
 
 #define LDAP_URL_PREFIX         "ldap://"
-#define LDAP_URL_PREFIX_LEN     7
-#define LDAP_URL_URLCOLON	"URL:"
-#define LDAP_URL_URLCOLON_LEN	4
+#define LDAP_URL_PREFIX_LEN     (sizeof(LDAP_URL_PREFIX)-1)
+#define LDAPS_URL_PREFIX		"ldaps://"
+#define LDAPS_URL_PREFIX_LEN	(sizeof(LDAPS_URL_PREFIX)-1)
+#define LDAP_URL_URLCOLON		"URL:"
+#define LDAP_URL_URLCOLON_LEN	(sizeof(LDAP_URL_URLCOLON)-1)
 #define NULLLDAPURLDESC ((LDAPURLDesc *)NULL)
 
 #define LDAP_REF_STR		"Referral:\n"
-#define LDAP_REF_STR_LEN	10
+#define LDAP_REF_STR_LEN	(sizeof(LDAP_REF_STR)-1)
 #define LDAP_LDAP_REF_STR	LDAP_URL_PREFIX
-#define LDAP_LDAP_REF_STR_LEN	LDAP_URL_PREFIX_LEN
-
-#define LDAP_DEFAULT_REFHOPLIMIT 5
+#define LDAP_LDAP_REF_STR_LEN	(sizeof(LDAP_LDAP_REF_STR)-1)
 
 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
 #define LDAP_DX_REF_STR		"dx://"
-#define LDAP_DX_REF_STR_LEN	5
+#define LDAP_DX_REF_STR_LEN	(sizeof(LDAP_DX_REF_STR)-1)
 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_DNS */
 
+#define LDAP_DEFAULT_REFHOPLIMIT 5
+
 #define LDAP_BOOL_REFERRALS		0
 #define LDAP_BOOL_RESTART		1
 #define LDAP_BOOL_DNS			2
@@ -269,12 +271,14 @@ void ldap_int_initialize LDAP_P((void));
 #define LDAP_INT_CALLOC(n,s)	(LBER_CALLOC((n),(s)))
 #define LDAP_INT_REALLOC(p,s)	(LBER_REALLOC((p),(s)))
 #define LDAP_INT_FREE(p)		(LBER_FREE((p)))
+#define LDAP_INT_VFREE(v)		(LBER_VFREE((v)))
 
 #ifndef LDAP_MALLOC
 #define LDAP_MALLOC(s)		(LBER_MALLOC((s)))
 #define LDAP_CALLOC(n,s)	(LBER_CALLOC((n),(s)))
 #define LDAP_REALLOC(p,s)	(LBER_REALLOC((p),(s)))
 #define LDAP_FREE(p)		(LBER_FREE((p)))
+#define LDAP_VFREE(v)		(LBER_VFREE((v)))
 #endif
 
 /*
diff --git a/libraries/libldap/schema.c b/libraries/libldap/schema.c
index 50fd9c4d39..3f06e8ce36 100644
--- a/libraries/libldap/schema.c
+++ b/libraries/libldap/schema.c
@@ -42,14 +42,17 @@ new_safe_string(int size)
 	ss = LDAP_MALLOC(sizeof(safe_string));
 	if ( !ss )
 		return(NULL);
-	ss->size = size;
-	ss->pos = 0;
+
 	ss->val = LDAP_MALLOC(size);
-	ss->at_whsp = 0;
 	if ( !ss->val ) {
 		LDAP_FREE(ss);
 		return(NULL);
 	}
+
+	ss->size = size;
+	ss->pos = 0;
+	ss->at_whsp = 0;
+
 	return ss;
 }
 
@@ -58,8 +61,8 @@ safe_string_free(safe_string * ss)
 {
 	if ( !ss )
 		return;
-	ldap_memfree(ss->val);
-	ldap_memfree(ss);
+	LDAP_FREE(ss->val);
+	LDAP_FREE(ss);
 }
 
 static char *
@@ -431,27 +434,6 @@ ldap_attributetype2str( LDAP_ATTRIBUTE_TYPE * at )
 	return(retstring);
 }
 
-/*
- * This is ripped from servers/slapd/charray.c that should be promoted
- * to -lldap or something so that it is used everywhere.
- */
-static void
-charray_free( char **array )
-{
-	char	**a;
-
-	if ( array == NULL ) {
-		return;
-	}
-
-	for ( a = array; *a != NULL; a++ ) {
-		if ( *a != NULL ) {
-			LDAP_FREE( *a );
-		}
-	}
-	LDAP_FREE( (char *) array );
-}
-
 /*
  * Now come the parsers.  There is one parser for each entity type:
  * objectclasses, attributetypes, etc.
@@ -644,7 +626,7 @@ parse_qdescrs(char **sp, int *code)
 					size++;
 					res1 = LDAP_REALLOC(res,size*sizeof(char *));
 					if ( !res1 ) {
-						charray_free(res);
+						LDAP_VFREE(res);
 						*code = LDAP_SCHERR_OUTOFMEM;
 						return(NULL);
 					}
@@ -654,7 +636,7 @@ parse_qdescrs(char **sp, int *code)
 				pos++;
 				parse_whsp(sp);
 			} else {
-				charray_free(res);
+				LDAP_VFREE(res);
 				*code = LDAP_SCHERR_UNEXPTOKEN;
 				return(NULL);
 			}
@@ -716,7 +698,7 @@ parse_noidlen(char **sp, int *code, int *len)
 		(*sp)++;
 		if ( **sp != '}' ) {
 			*code = LDAP_SCHERR_UNEXPTOKEN;
-			ldap_memfree(sval);
+			LDAP_FREE(sval);
 			return NULL;
 		}
 		(*sp)++;
@@ -766,7 +748,7 @@ parse_oids(char **sp, int *code)
 			pos++;
 		} else {
 			*code = LDAP_SCHERR_UNEXPTOKEN;
-			charray_free(res);
+			LDAP_VFREE(res);
 			return NULL;
 		}
 		parse_whsp(sp);
@@ -783,7 +765,7 @@ parse_oids(char **sp, int *code)
 						size++;
 						res1 = LDAP_REALLOC(res,size*sizeof(char *));
 						if ( !res1 ) {
-						  charray_free(res);
+						  LDAP_VFREE(res);
 						  *code = LDAP_SCHERR_OUTOFMEM;
 						  return(NULL);
 						}
@@ -793,13 +775,13 @@ parse_oids(char **sp, int *code)
 					pos++;
 				} else {
 					*code = LDAP_SCHERR_UNEXPTOKEN;
-					charray_free(res);
+					LDAP_VFREE(res);
 					return NULL;
 				}
 				parse_whsp(sp);
 			} else {
 				*code = LDAP_SCHERR_UNEXPTOKEN;
-				charray_free(res);
+				LDAP_VFREE(res);
 				return NULL;
 			}
 		}
@@ -825,9 +807,9 @@ parse_oids(char **sp, int *code)
 static void
 free_syn(LDAP_SYNTAX * syn)
 {
-	ldap_memfree(syn->syn_oid);
-	ldap_memfree(syn->syn_desc);
-	ldap_memfree(syn);
+	LDAP_FREE(syn->syn_oid);
+	LDAP_FREE(syn->syn_desc);
+	LDAP_FREE(syn);
 }
 
 LDAP_SYNTAX *
@@ -930,15 +912,15 @@ ldap_str2syntax( char * s, int * code, char ** errp )
 static void
 free_at(LDAP_ATTRIBUTE_TYPE * at)
 {
-	ldap_memfree(at->at_oid);
-	charray_free(at->at_names);
-	ldap_memfree(at->at_desc);
-	ldap_memfree(at->at_sup_oid);
-	ldap_memfree(at->at_equality_oid);
-	ldap_memfree(at->at_ordering_oid);
-	ldap_memfree(at->at_substr_oid);
-	ldap_memfree(at->at_syntax_oid);
-	ldap_memfree(at);
+	LDAP_FREE(at->at_oid);
+	LDAP_VFREE(at->at_names);
+	LDAP_FREE(at->at_desc);
+	LDAP_FREE(at->at_sup_oid);
+	LDAP_FREE(at->at_equality_oid);
+	LDAP_FREE(at->at_ordering_oid);
+	LDAP_FREE(at->at_substr_oid);
+	LDAP_FREE(at->at_syntax_oid);
+	LDAP_FREE(at);
 }
 
 LDAP_ATTRIBUTE_TYPE *
@@ -1212,13 +1194,13 @@ ldap_str2attributetype( char * s, int * code, char ** errp )
 static void
 free_oc(LDAP_OBJECT_CLASS * oc)
 {
-	ldap_memfree(oc->oc_oid);
-	charray_free(oc->oc_names);
-	ldap_memfree(oc->oc_desc);
-	charray_free(oc->oc_sup_oids);
-	charray_free(oc->oc_at_oids_must);
-	charray_free(oc->oc_at_oids_may);
-	ldap_memfree(oc);
+	LDAP_FREE(oc->oc_oid);
+	LDAP_VFREE(oc->oc_names);
+	LDAP_FREE(oc->oc_desc);
+	LDAP_VFREE(oc->oc_sup_oids);
+	LDAP_VFREE(oc->oc_at_oids_must);
+	LDAP_VFREE(oc->oc_at_oids_may);
+	LDAP_FREE(oc);
 }
 
 LDAP_OBJECT_CLASS *
-- 
GitLab