From 78618653c23168b0cc143eca54264191fa5a8bd6 Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@openldap.org>
Date: Tue, 5 Jul 2022 03:49:34 +0100
Subject: [PATCH] ITS#9876 Coverity fixes: plug memleaks

---
 libraries/libldap/deref.c |  1 +
 libraries/libldap/ldif.c  |  3 ++-
 libraries/libldap/turn.c  | 12 ++++++------
 libraries/libldap/txn.c   | 12 ++++++------
 4 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/libraries/libldap/deref.c b/libraries/libldap/deref.c
index 801954eb9..7d3471ad3 100644
--- a/libraries/libldap/deref.c
+++ b/libraries/libldap/deref.c
@@ -193,6 +193,7 @@ ldap_parse_derefresponse_control(
 		dr = LDAP_CALLOC( 1, sizeof(LDAPDerefRes) );
 		if ( dr == NULL ) {
 			ldap_derefresponse_free( drhead );
+			ber_free( ber, 1 );
 			*drp2 = NULL;
 			ld->ld_errno = LDAP_NO_MEMORY;
 			return ld->ld_errno;
diff --git a/libraries/libldap/ldif.c b/libraries/libldap/ldif.c
index 900a97960..57e44f8c7 100644
--- a/libraries/libldap/ldif.c
+++ b/libraries/libldap/ldif.c
@@ -729,7 +729,8 @@ ldif_open(
 	if ( fp ) {
 		lfp = ber_memalloc( sizeof( LDIFFP ));
 		if ( lfp == NULL ) {
-		    return NULL;
+			fclose( fp );
+			return NULL;
 		}
 		lfp->fp = fp;
 		lfp->prev = NULL;
diff --git a/libraries/libldap/turn.c b/libraries/libldap/turn.c
index 565b449af..7725f01d0 100644
--- a/libraries/libldap/turn.c
+++ b/libraries/libldap/turn.c
@@ -44,7 +44,7 @@ ldap_turn(
 {
 #ifdef LDAP_EXOP_X_TURN
 	BerElement *turnvalber = NULL;
-	struct berval *turnvalp = NULL;
+	struct berval turnval;
 	int rc;
 
 	turnvalber = ber_alloc_t( LBER_USE_DER );
@@ -53,10 +53,10 @@ ldap_turn(
 	} else {
 		ber_printf( turnvalber, "{s}", identifier );
 	}
-	ber_flatten( turnvalber, &turnvalp );
+	ber_flatten2( turnvalber, &turnval, 0 );
 
 	rc = ldap_extended_operation( ld, LDAP_EXOP_X_TURN,
-			turnvalp, sctrls, cctrls, msgidp );
+			&turnval, sctrls, cctrls, msgidp );
 	ber_free( turnvalber, 1 );
 	return rc;
 #else
@@ -74,7 +74,7 @@ ldap_turn_s(
 {
 #ifdef LDAP_EXOP_X_TURN
 	BerElement *turnvalber = NULL;
-	struct berval *turnvalp = NULL;
+	struct berval turnval;
 	int rc;
 
 	turnvalber = ber_alloc_t( LBER_USE_DER );
@@ -83,10 +83,10 @@ ldap_turn_s(
 	} else {
 		ber_printf( turnvalber, "{s}", identifier );
 	}
-	ber_flatten( turnvalber, &turnvalp );
+	ber_flatten2( turnvalber, &turnval, 0 );
 
 	rc = ldap_extended_operation_s( ld, LDAP_EXOP_X_TURN,
-			turnvalp, sctrls, cctrls, NULL, NULL );
+			&turnval, sctrls, cctrls, NULL, NULL );
 	ber_free( turnvalber, 1 );
 	return rc;
 #else
diff --git a/libraries/libldap/txn.c b/libraries/libldap/txn.c
index 66b22e873..640900234 100644
--- a/libraries/libldap/txn.c
+++ b/libraries/libldap/txn.c
@@ -68,7 +68,7 @@ ldap_txn_end(
 {
 	int rc;
 	BerElement *txnber = NULL;
-	struct berval *txnval = NULL;
+	struct berval txnval;
 
 	assert( txnid != NULL );
 
@@ -80,10 +80,10 @@ ldap_txn_end(
 		ber_printf( txnber, "{bON}", commit, txnid );
 	}
 
-	ber_flatten( txnber, &txnval );
+	ber_flatten2( txnber, &txnval, 0 );
 
 	rc = ldap_extended_operation( ld, LDAP_EXOP_TXN_END,
-		txnval, sctrls, cctrls, msgidp );
+		&txnval, sctrls, cctrls, msgidp );
 
 	ber_free( txnber, 1 );
 	return rc;
@@ -100,7 +100,7 @@ ldap_txn_end_s(
 {
 	int rc;
 	BerElement *txnber = NULL;
-	struct berval *txnval = NULL;
+	struct berval txnval;
 	struct berval *retdata = NULL;
 
 	if ( retidp != NULL ) *retidp = -1;
@@ -113,10 +113,10 @@ ldap_txn_end_s(
 		ber_printf( txnber, "{bON}", commit, txnid );
 	}
 
-	ber_flatten( txnber, &txnval );
+	ber_flatten2( txnber, &txnval, 0 );
 
 	rc = ldap_extended_operation_s( ld, LDAP_EXOP_TXN_END,
-		txnval, sctrls, cctrls, NULL, &retdata );
+		&txnval, sctrls, cctrls, NULL, &retdata );
 
 	ber_free( txnber, 1 );
 
-- 
2.33.0

