From 64680748c6ee8be12b6fdf59bb1d377697b19c66 Mon Sep 17 00:00:00 2001
From: Noriko Hosoi <nhosoi@redhat.com>
Date: Thu, 27 Aug 2015 17:04:53 -0700
Subject: [PATCH] Ticket #48252 - db2index creates index entry from deleted
 records

Description: Commit ca4d38dff3bd820af2bf60c9bcc82fd64aac2556 fixing
ticket #48252 introduced memory leaks.
. nstombstone_vals needs to be allocated just once and freed at the end.
. when skipping to index an entry for being a tombstone, the entry needs
  to be freed.
---
 ldap/servers/slapd/back-ldbm/ldif2ldbm.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/ldap/servers/slapd/back-ldbm/ldif2ldbm.c b/ldap/servers/slapd/back-ldbm/ldif2ldbm.c
index 3d07b2e..150a008 100644
--- a/ldap/servers/slapd/back-ldbm/ldif2ldbm.c
+++ b/ldap/servers/slapd/back-ldbm/ldif2ldbm.c
@@ -1679,6 +1679,7 @@ ldbm_back_ldbm2index(Slapi_PBlock *pb)
     back_txn         txn;
     ID               suffixid = NOID; /* holds the id of the suffix entry */
     Slapi_Value      **nstombstone_vals = NULL;
+    int              istombstone = 0;
 
     LDAPDebug( LDAP_DEBUG_TRACE, "=> ldbm_back_ldbm2index\n", 0, 0, 0 );
     if ( g_get_shutdown() || c_get_shutdown() ) {
@@ -2163,8 +2164,11 @@ ldbm_back_ldbm2index(Slapi_PBlock *pb)
             const CSN *tombstone_csn = entry_get_deletion_csn(ep->ep_entry);
             char deletion_csn_str[CSN_STRSIZE];
 
-            nstombstone_vals = (Slapi_Value **) slapi_ch_calloc(2, sizeof(Slapi_Value *));
-            *nstombstone_vals = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
+            istombstone = 1;
+            if (!nstombstone_vals) {
+                nstombstone_vals = (Slapi_Value **) slapi_ch_calloc(2, sizeof(Slapi_Value *));
+                *nstombstone_vals = slapi_value_new_string(SLAPI_ATTR_VALUE_TOMBSTONE);
+            }
             if (tombstone_csn) {
                 if (!run_from_cmdline) {
                     rc = dblayer_txn_begin(be, NULL, &txn);
@@ -2219,15 +2223,18 @@ ldbm_back_ldbm2index(Slapi_PBlock *pb)
                     }
                 }
             }
+        } else {
+            istombstone = 0;
         }
 
         /*
          * Update the attribute indexes
          */
         if (indexAttrs) {
-            if (nstombstone_vals && !(index_ext & (DB2INDEX_ENTRYRDN|DB2INDEX_OBJECTCLASS))) {
+            if (istombstone && !(index_ext & (DB2INDEX_ENTRYRDN|DB2INDEX_OBJECTCLASS))) {
                 /* if it is a tombstone entry, just entryrdn or "objectclass: nstombstone"
                  * need to be reindexed.  the to-be-indexed list does not contain them. */
+                backentry_free( &ep );
                 continue;
             }
             for (i = slapi_entry_first_attr(ep->ep_entry, &attr); i == 0;
@@ -2241,7 +2248,7 @@ ldbm_back_ldbm2index(Slapi_PBlock *pb)
                         goto err_out;
                     }
                     if (slapi_attr_type_cmp(indexAttrs[j], type, SLAPI_TYPE_CMP_SUBTYPE) == 0) {
-                        if (nstombstone_vals) {
+                        if (istombstone) {
                             if (!slapi_attr_type_cmp(indexAttrs[j], SLAPI_ATTR_OBJECTCLASS, SLAPI_TYPE_CMP_SUBTYPE)) {
                                 is_tombstone_obj = 1; /* is tombstone && is objectclass. need to index "nstombstone"*/
                             } else if (slapi_attr_type_cmp(indexAttrs[j], LDBM_ENTRYRDN_STR, SLAPI_TYPE_CMP_SUBTYPE)) {
@@ -2325,7 +2332,7 @@ ldbm_back_ldbm2index(Slapi_PBlock *pb)
         /*
          * If it is NOT a tombstone entry, update the Virtual List View indexes.
          */
-        for (vlvidx = 0; !nstombstone_vals && (vlvidx < numvlv); vlvidx++) {
+        for (vlvidx = 0; !istombstone && (vlvidx < numvlv); vlvidx++) {
             char *ai = "Unknown index";
 
             if ( g_get_shutdown() || c_get_shutdown() ) {
@@ -2550,6 +2557,7 @@ err_min:
         }
     }
 
+    valuearray_free(&nstombstone_vals);
     if (indexAttrs) {
         slapi_ch_free((void **)&indexAttrs);
     }
-- 
1.9.3

