From 37636eabd33eb03ce3a90a7cc82901800aaf51ac Mon Sep 17 00:00:00 2001
From: Kurt Zeilenga <kurt@openldap.org>
Date: Mon, 31 May 1999 17:30:22 +0000
Subject: [PATCH] Clean up LDAP_BOOL_GET and fetching via ldap_get_option().
 Modify apitest to test for non-zero instead of LDAP_OPT_ON.

---
 libraries/liblber/memory.c   | 20 ++++++++++++++++++++
 libraries/libldap/apitest.c  |  6 ++----
 libraries/libldap/ldap-int.h |  2 +-
 libraries/libldap/options.c  | 21 +++++++++------------
 libraries/libldap/request.c  |  2 +-
 libraries/libldap/result.c   |  6 ++----
 6 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c
index d4bbdde8bc..59e2258d3c 100644
--- a/libraries/liblber/memory.c
+++ b/libraries/liblber/memory.c
@@ -16,8 +16,14 @@ ber_memfree( void *p )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
+	/* catch p == NULL when debugging */
 	assert( p != NULL );
 
+	/* ignore p == NULL when not debugging */
+	if( p == NULL ) {
+		return;
+	}
+
 	if( ber_int_memory_fns == NULL ) {
 		free( p );
 		return;
@@ -33,8 +39,14 @@ ber_memalloc( size_t s )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
+	/* catch s == 0 when debugging */
 	assert( s );
 
+	/* ignore s == 0 when not debugging */
+	if( s == 0 ) {
+		return NULL;
+	}
+
 	if( ber_int_memory_fns == NULL ) {
 		return malloc( s );
 	}
@@ -49,8 +61,14 @@ ber_memcalloc( size_t n, size_t s )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
+	/* catch s,n == 0 when debugging */
 	assert( n && s );
 
+	/* ignore s,n == 0 when not debugging */
+	if( n == 0 || s == 0 ) {
+		return NULL;
+	}
+
 	if( ber_int_memory_fns == NULL ) {
 		return calloc( n, s );
 	}
@@ -65,10 +83,12 @@ ber_memrealloc( void* p, size_t s )
 {
     ber_int_options.lbo_valid = LBER_INITIALIZED;
 
+	/* realloc(NULL,s) -> malloc(s) */
 	if( p == NULL ) {
 		return ber_memalloc( s );
 	}
 	
+	/* realloc(p,0) -> free(p) */
 	if( s == 0 ) {
 		ber_memfree( p );
 		return NULL;
diff --git a/libraries/libldap/apitest.c b/libraries/libldap/apitest.c
index 0274f39280..e27028a8d6 100644
--- a/libraries/libldap/apitest.c
+++ b/libraries/libldap/apitest.c
@@ -162,15 +162,13 @@ main(int argc, char **argv)
 		fprintf(stderr, "%s: ldap_get_option(referrals) failed\n", argv[0]);
 		return EXIT_FAILURE;
 	}
-	printf("  REFERRALS:         %s\n",
-		ival == (int) LDAP_OPT_ON ? "on" : "off");
+	printf("  REFERRALS:         %s\n", ival ? "on" : "off");
 
 	if(ldap_get_option(NULL, LDAP_OPT_RESTART, &ival) != LDAP_SUCCESS) {
 		fprintf(stderr, "%s: ldap_get_option(restart) failed\n", argv[0]);
 		return EXIT_FAILURE;
 	}
-	printf("  RESTART:           %s\n",
-		ival == (int) LDAP_OPT_ON ? "on" : "off");
+	printf("  RESTART:           %s\n", ival ? "on" : "off");
 
 	if(ldap_get_option(NULL, LDAP_OPT_PROTOCOL_VERSION, &ival) != LDAP_SUCCESS) {
 		fprintf(stderr, "%s: ldap_get_option(protocol version) failed\n", argv[0]);
diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h
index bd07b0b4ab..684f3dc024 100644
--- a/libraries/libldap/ldap-int.h
+++ b/libraries/libldap/ldap-int.h
@@ -56,7 +56,7 @@ LDAP_BEGIN_DECL
 #define LDAP_BOOLEANS	unsigned long
 #define LDAP_BOOL(n)	(1 << (n))
 #define LDAP_BOOL_GET(lo, bool)	((lo)->ldo_booleans & LDAP_BOOL(bool) \
-									?  LDAP_OPT_ON : LDAP_OPT_OFF)
+									?  -1 : 0)
 #define LDAP_BOOL_SET(lo, bool) ((lo)->ldo_booleans |= LDAP_BOOL(bool))
 #define LDAP_BOOL_CLR(lo, bool) ((lo)->ldo_booleans &= ~LDAP_BOOL(bool))
 #define LDAP_BOOL_ZERO(lo) ((lo)->ldo_booleans = 0)
diff --git a/libraries/libldap/options.c b/libraries/libldap/options.c
index 149c83cc30..f03382519e 100644
--- a/libraries/libldap/options.c
+++ b/libraries/libldap/options.c
@@ -167,18 +167,15 @@ ldap_get_option(
 		return LDAP_OPT_SUCCESS;
 
 	case LDAP_OPT_REFERRALS:
-		* (int *) outvalue = (LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS) ==
-				      LDAP_OPT_ON);
+		* (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_REFERRALS);
 		return LDAP_OPT_SUCCESS;
 		
 	case LDAP_OPT_RESTART:
-		* (int *) outvalue = (LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART) ==
-				      LDAP_OPT_ON);
+		* (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_RESTART);
 		return LDAP_OPT_SUCCESS;
 
 	case LDAP_OPT_DNS:	/* LDAPv2 */
-		* (int *) outvalue = (LDAP_BOOL_GET(lo, LDAP_BOOL_DNS) ==
-				      LDAP_OPT_ON);
+		* (int *) outvalue = (int) LDAP_BOOL_GET(lo, LDAP_BOOL_DNS);
 		return LDAP_OPT_SUCCESS;
 
 	case LDAP_OPT_PROTOCOL_VERSION:
@@ -320,18 +317,18 @@ ldap_set_option(
 
 	switch(option) {
 	case LDAP_OPT_REFERRALS:
-		if(invalue == LDAP_OPT_ON) {
-			LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
-		} else {
+		if(invalue == LDAP_OPT_OFF) {
 			LDAP_BOOL_CLR(lo, LDAP_BOOL_REFERRALS);
+		} else {
+			LDAP_BOOL_SET(lo, LDAP_BOOL_REFERRALS);
 		}
 		return LDAP_OPT_SUCCESS;
 
 	case LDAP_OPT_RESTART:
-		if(invalue == LDAP_OPT_ON) {
-			LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
-		} else {
+		if(invalue == LDAP_OPT_OFF) {
 			LDAP_BOOL_CLR(lo, LDAP_BOOL_RESTART);
+		} else {
+			LDAP_BOOL_SET(lo, LDAP_BOOL_RESTART);
 		}
 		return LDAP_OPT_SUCCESS;
 	}
diff --git a/libraries/libldap/request.c b/libraries/libldap/request.c
index 5c5c009525..3f7970f7cc 100644
--- a/libraries/libldap/request.c
+++ b/libraries/libldap/request.c
@@ -110,7 +110,7 @@ ldap_send_initial_request(
 
 
 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_DNS
-	if (( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ) == LDAP_OPT_ON )
+	if ( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_DNS ))
 		&& ldap_is_dns_dn( dn ) )
 	{
 		if (( servers = dn2servers( ld, dn )) == NULL ) {
diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c
index aaf58fde24..81c12f80ce 100644
--- a/libraries/libldap/result.c
+++ b/libraries/libldap/result.c
@@ -196,8 +196,7 @@ wait4msg( LDAP *ld, int msgid, int all, struct timeval *timeout,
 #endif
 
 			if ( rc == 0 || ( rc == -1 && (
-				( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
-					== LDAP_OPT_OFF )
+				!LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_RESTART)
 				|| errno != EINTR )))
 			{
 				ld->ld_errno = (rc == -1 ? LDAP_SERVER_DOWN :
@@ -341,8 +340,7 @@ try_read1msg( LDAP *ld, int msgid, int all, Sockbuf *sb,
 	if ( tag != LDAP_RES_SEARCH_ENTRY ) {
 		if ( ld->ld_version >= LDAP_VERSION2 &&
 			( lr->lr_parent != NULL ||
-			( LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS)
-				!= LDAP_OPT_OFF ) ) )
+			LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_REFERRALS) ) )
 		{
 			tmpber = *ber;	/* struct copy */
 			if ( ber_scanf( &tmpber, "{iaa}", &lderr,
-- 
GitLab