From 973a2b41b7abcb4bd57cccfe1cc9b0bfc10a441f Mon Sep 17 00:00:00 2001
From: Hallvard Furuseth <hallvard@openldap.org>
Date: Tue, 24 Jul 2007 20:53:23 +0000
Subject: [PATCH] Make LDAP_MEMORY_DEBUG a bitmask. #define
 LDAP_MEMORY_DEBUG_ASSERT. ITS#4990.

---
 libraries/liblber/io.c       |  5 +----
 libraries/liblber/lber-int.h |  5 +++++
 libraries/liblber/memory.c   | 18 +++++++-----------
 libraries/libldap/controls.c |  8 ++------
 4 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c
index 6e929110f5..4f4afa390f 100644
--- a/libraries/liblber/io.c
+++ b/libraries/liblber/io.c
@@ -204,11 +204,8 @@ ber_free_buf( BerElement *ber )
 void
 ber_free( BerElement *ber, int freebuf )
 {
-#ifdef LDAP_MEMORY_DEBUG
-	assert( ber != NULL );
-#endif
-
 	if( ber == NULL ) {
+		LDAP_MEMORY_DEBUG_ASSERT( ber != NULL );
 		return;
 	}
 
diff --git a/libraries/liblber/lber-int.h b/libraries/liblber/lber-int.h
index 24d0dac092..e2657d2eb1 100644
--- a/libraries/liblber/lber-int.h
+++ b/libraries/liblber/lber-int.h
@@ -47,6 +47,11 @@ LBER_V (BER_ERRNO_FN) ber_int_errno_fn;
 #ifdef LDAP_MEMORY_DEBUG
 LBER_V (long)	ber_int_meminuse;
 #endif
+#if defined(LDAP_MEMORY_DEBUG) && ((LDAP_MEMORY_DEBUG +0) & 2)
+# define LDAP_MEMORY_DEBUG_ASSERT assert
+#else
+# define LDAP_MEMORY_DEBUG_ASSERT(expr) ((void) 0)
+#endif
 
 struct lber_options {
 	short lbo_valid;
diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c
index 3f569f7f8a..94ef772d97 100644
--- a/libraries/liblber/memory.c
+++ b/libraries/liblber/memory.c
@@ -28,9 +28,11 @@
 /*
  * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
  * debugging memory management within OpenLDAP libraries and slapd.
- * It should only be enabled by an experienced developer as it
- * causes the inclusion of numerous assert()'s, many of which may
- * be triggered by a prefectly valid program.
+ *
+ * It should only be enabled by an experienced developer as it causes
+ * the inclusion of numerous assert()'s, many of which may be triggered
+ * by a prefectly valid program.  If LDAP_MEMORY_DEBUG & 2 is true,
+ * that includes asserts known to break both slapd and current clients.
  *
  * The code behind this macro is subject to change as needed to
  * support this testing.
@@ -192,11 +194,8 @@ ber_memalloc_x( ber_len_t s, void *ctx )
 {
 	void *new;
 
-#ifdef LDAP_MEMORY_DEBUG
-	assert( s != 0 );
-#endif
-
 	if( s == 0 ) {
+		LDAP_MEMORY_DEBUG_ASSERT( s != 0 );
 		return NULL;
 	}
 
@@ -248,11 +247,8 @@ ber_memcalloc_x( ber_len_t n, ber_len_t s, void *ctx )
 {
 	void *new;
 
-#ifdef LDAP_MEMORY_DEBUG
-	assert( n != 0 && s != 0);
-#endif
-
 	if( n == 0 || s == 0 ) {
+		LDAP_MEMORY_DEBUG_ASSERT( n != 0 && s != 0);
 		return NULL;
 	}
 
diff --git a/libraries/libldap/controls.c b/libraries/libldap/controls.c
index faca9839ec..51fde85a51 100644
--- a/libraries/libldap/controls.c
+++ b/libraries/libldap/controls.c
@@ -246,9 +246,7 @@ int ldap_pvt_get_controls(
 void
 ldap_control_free( LDAPControl *c )
 {
-#ifdef LDAP_MEMORY_DEBUG
-	assert( c != NULL );
-#endif
+	LDAP_MEMORY_DEBUG_ASSERT( c != NULL );
 
 	if ( c != NULL ) {
 		if( c->ldctl_oid != NULL) {
@@ -269,9 +267,7 @@ ldap_control_free( LDAPControl *c )
 void
 ldap_controls_free( LDAPControl **controls )
 {
-#ifdef LDAP_MEMORY_DEBUG
-	assert( controls != NULL );
-#endif
+	LDAP_MEMORY_DEBUG_ASSERT( controls != NULL );
 
 	if ( controls != NULL ) {
 		int i;
-- 
GitLab