From bed646552971a1cd2c44394b8c4bbbe873d1a22c Mon Sep 17 00:00:00 2001
From: Kurt Zeilenga <kurt@openldap.org>
Date: Wed, 2 Jun 1999 21:38:48 +0000
Subject: [PATCH] Make first argument of *_get_option const (experimental).
 Make _MOD_SOFTADD 0x1000 to minimize chance of conflict with legit changes to
 API spec. Fix memory leak in ldap_mods_free() and minor memory allocator
 issues.

---
 include/lber.h               |  2 +-
 include/ldap.h               |  7 ++++---
 libraries/liblber/lber-int.h |  2 +-
 libraries/liblber/options.c  | 14 +++++++-------
 libraries/libldap/free.c     | 13 ++++++++++---
 libraries/libldap/options.c  |  4 ++--
 servers/slapd/slap.h         |  2 +-
 7 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/include/lber.h b/include/lber.h
index 9d3db08e2e..e4f3178d1e 100644
--- a/include/lber.h
+++ b/include/lber.h
@@ -396,7 +396,7 @@ ber_flatten LDAP_P((
 
 LDAP_F( int )
 ber_get_option LDAP_P((
-	void *item,
+	LDAP_CONST void *item,
 	int option,
 	void *outvalue));
 
diff --git a/include/ldap.h b/include/ldap.h
index d1d5008bce..2092565bd7 100644
--- a/include/ldap.h
+++ b/include/ldap.h
@@ -318,11 +318,12 @@ typedef struct ldapmod {
 #define LDAP_MOD_ADD		0x0000
 #define LDAP_MOD_DELETE		0x0001
 #define LDAP_MOD_REPLACE	0x0002
-/* IMPORTANT: do not use code 0x04, it is used internally by the backends!
+#define LDAP_MOD_BVALUES	0x0080
+/* IMPORTANT: do not use code 0x1000 (or above),
+ * it is used internally by the backends!
  * (see ldap/servers/slapd/slap.h)
  * JCG 05/1999 (gomez@engr.sgi.com)
  */
-#define LDAP_MOD_BVALUES	0x0080
 	char		*mod_type;
 	union mod_vals_u {
 		char		**modv_strvals;
@@ -506,7 +507,7 @@ struct timeval;
  */
 LDAP_F( int )
 ldap_get_option LDAP_P((
-	LDAP *ld,
+	LDAP_CONST LDAP *ld,
 	int option,
 	void *outvalue));
 
diff --git a/libraries/liblber/lber-int.h b/libraries/liblber/lber-int.h
index 2a8349aff6..8cb299ef95 100644
--- a/libraries/liblber/lber-int.h
+++ b/libraries/liblber/lber-int.h
@@ -216,8 +216,8 @@ ber_pvt_sb_init LDAP_P(( Sockbuf *sb ));
 
 LDAP_F(	int )
 ber_pvt_sb_destroy LDAP_P(( Sockbuf *sb ));
-#ifdef USE_SASL
 
+#ifdef USE_SASL
 LDAP_F( int )
 ber_pvt_sb_set_sec LDAP_P(( Sockbuf *sb, Sockbuf_Sec *sec, void *arg ));
 
diff --git a/libraries/liblber/options.c b/libraries/liblber/options.c
index 863f9396d5..a323bbee96 100644
--- a/libraries/liblber/options.c
+++ b/libraries/liblber/options.c
@@ -14,12 +14,12 @@ struct lber_options ber_int_options = {
 
 int
 ber_get_option(
-	void	*item,
+	LDAP_CONST void	*item,
 	int		option,
 	void	*outvalue)
 {
-	BerElement *ber;
-	Sockbuf *sb;
+	LDAP_CONST BerElement *ber;
+	LDAP_CONST Sockbuf *sb;
 
 	ber_int_options.lbo_valid = LBER_INITIALIZED;
 
@@ -37,8 +37,8 @@ ber_get_option(
 		return LBER_OPT_ERROR;
 	}
 
-	ber = (BerElement *) item;
-	sb = (Sockbuf *) item;
+	ber = item;
+	sb = item;
 
 	switch(option) {
 	case LBER_OPT_BER_OPTIONS:
@@ -115,8 +115,8 @@ ber_set_option(
 		return LBER_OPT_ERROR;
 	}
 
-	ber = (BerElement *) item;
-	sb = (Sockbuf *) item;
+	ber = item;
+	sb = item;
 
 	switch(option) {
 	case LBER_OPT_BER_OPTIONS:
diff --git a/libraries/libldap/free.c b/libraries/libldap/free.c
index 2d5b390a16..ba8c70902b 100644
--- a/libraries/libldap/free.c
+++ b/libraries/libldap/free.c
@@ -106,10 +106,17 @@ ldap_mods_free( LDAPMod **mods, int freemods )
 
 	for ( i = 0; mods[i] != NULL; i++ ) {
 		if ( mods[i]->mod_op & LDAP_MOD_BVALUES ) {
-			ber_bvecfree( mods[i]->mod_bvalues );
-		} else {
-			ldap_value_free( mods[i]->mod_values );
+			if( mods[i]->mod_bvalues != NULL )
+				ber_bvecfree( mods[i]->mod_bvalues );
+
+		} else if( mods[i]->mod_values != NULL ) {
+			LDAP_VFREE( mods[i]->mod_values );
+		}
+
+		if ( mods[i]->mod_type != NULL ) {
+			LDAP_FREE( mods[i]->mod_type );
 		}
+
 		LDAP_FREE( (char *) mods[i] );
 	}
 
diff --git a/libraries/libldap/options.c b/libraries/libldap/options.c
index f03382519e..e659643982 100644
--- a/libraries/libldap/options.c
+++ b/libraries/libldap/options.c
@@ -77,11 +77,11 @@ static const LDAPAPIFeatureInfo features[] = {
 
 int
 ldap_get_option(
-	LDAP	*ld,
+	LDAP_CONST LDAP	*ld,
 	int		option,
 	void	*outvalue)
 {
-	struct ldapoptions *lo;
+	LDAP_CONST struct ldapoptions *lo;
 
 	if( ldap_int_global_options.ldo_valid != LDAP_INITIALIZED ) {
 		ldap_int_initialize();
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
index 8f5f692519..0f5eec9e27 100644
--- a/servers/slapd/slap.h
+++ b/servers/slapd/slap.h
@@ -36,7 +36,7 @@
  * modrdn when the new rdn was already an attribute value itself.
  * JCG 05/1999 (gomez@engr.sgi.com)
  */
-#define LDAP_MOD_SOFTADD	0x04
+#define LDAP_MOD_SOFTADD	0x1000
 
 #define DN_DNS	0
 #define DN_X500	1
-- 
GitLab