]> git.sur5r.net Git - openldap/commitdiff
Fixed some memory allocation/freeing bugs
authorRalf Haferkamp <ralf@openldap.org>
Wed, 14 Nov 2001 17:33:54 +0000 (17:33 +0000)
committerRalf Haferkamp <ralf@openldap.org>
Wed, 14 Nov 2001 17:33:54 +0000 (17:33 +0000)
21 files changed:
contrib/ldapc++/src/LDAPAddRequest.cpp
contrib/ldapc++/src/LDAPAsynConnection.cpp
contrib/ldapc++/src/LDAPAttribute.cpp
contrib/ldapc++/src/LDAPAttributeList.cpp
contrib/ldapc++/src/LDAPBindRequest.cpp
contrib/ldapc++/src/LDAPCompareRequest.cpp
contrib/ldapc++/src/LDAPControl.cpp
contrib/ldapc++/src/LDAPControl.h
contrib/ldapc++/src/LDAPControlSet.cpp
contrib/ldapc++/src/LDAPControlSet.h
contrib/ldapc++/src/LDAPDeleteRequest.cpp
contrib/ldapc++/src/LDAPEntry.cpp
contrib/ldapc++/src/LDAPExtRequest.cpp
contrib/ldapc++/src/LDAPMessageQueue.cpp
contrib/ldapc++/src/LDAPModDNRequest.cpp
contrib/ldapc++/src/LDAPModList.cpp
contrib/ldapc++/src/LDAPModification.cpp
contrib/ldapc++/src/LDAPModifyRequest.cpp
contrib/ldapc++/src/LDAPResult.cpp
contrib/ldapc++/src/LDAPSearchRequest.cpp
contrib/ldapc++/src/StringList.cpp

index 0cad59ec11c6bf43672c7ebfb34143d41b691a7a..b3a1a4d099373699d34294e004cbe8ec66a3fcac 100644 (file)
@@ -50,8 +50,8 @@ LDAPMessageQueue* LDAPAddRequest::sendRequest(){
     LDAPControl** tmpClCtrls = m_cons->getClCtrlsArray();
     int err=ldap_add_ext(m_connection->getSessionHandle(),
             m_entry->getDN().c_str(),attrs,tmpSrvCtrls,tmpClCtrls,&msgID);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
     ldap_mods_free(attrs,1);
     if(err != LDAP_SUCCESS){
         throw LDAPException(err);
index 095061d676eb6324176b2e2a56af38f995594399..2c1be7a29970226e53a3e67b08cae60696534391 100644 (file)
@@ -211,8 +211,8 @@ void LDAPAsynConnection::unbind(){
         LDAPControl** tmpClCtrls=m_constr->getClCtrlsArray();
         int err=ldap_unbind_ext(cur_session, tmpSrvCtrls, tmpClCtrls);
         cur_session=0;
-        ldap_controls_free(tmpSrvCtrls);
-        ldap_controls_free(tmpClCtrls);
+        LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+        LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
         if(err != LDAP_SUCCESS){
             throw LDAPException(err);
         }
index d5914024171a4266a336ad39e0f2a36162707e7b..6671414344e5dc21f93a36809ecc6f494d55718e 100644 (file)
@@ -123,18 +123,18 @@ const StringList& LDAPAttribute::getValues() const{
 
 BerValue** LDAPAttribute::getBerValues() const{
     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::getBerValues()" << endl);
-       size_t size=m_values.size();
+    size_t size=m_values.size();
     if (size == 0){
         return 0;
     }else{
-        BerValue **temp = new BerValue*[size+1];
+        BerValue **temp = (BerValue**) malloc(sizeof(BerValue*) * (size+1));
         StringList::const_iterator i;
         int p=0;
 
         for(i=m_values.begin(), p=0; i!=m_values.end(); i++,p++){
-            temp[p]=new BerValue;
+            temp[p]=(BerValue*) malloc(sizeof(BerValue));
             temp[p]->bv_len= i->size();
-            temp[p]->bv_val= new char[i->size()+1];
+            temp[p]->bv_val= (char*) malloc(sizeof(char) * (i->size()+1));
             i->copy(temp[p]->bv_val,string::npos);
         }
         temp[size]=0;
@@ -162,37 +162,37 @@ void LDAPAttribute::setName(const string& name){
 // The bin-FLAG of the mod_op  is always set to LDAP_MOD_BVALUES (0x80) 
 LDAPMod* LDAPAttribute::toLDAPMod() const {
     DEBUG(LDAP_DEBUG_TRACE, "LDAPAttribute::toLDAPMod()" << endl);
-       LDAPMod* ret=new LDAPMod();
-       ret->mod_op=LDAP_MOD_BVALUES;   //always assume binary-Values
-       ret->mod_type= new char[m_name.size()+1];
+    LDAPMod* ret= (LDAPMod*) malloc(sizeof(LDAPMod));
+    ret->mod_op=LDAP_MOD_BVALUES;      //always assume binary-Values
+    ret->mod_type= (char*) malloc(sizeof(char) * (m_name.size()+1));
     m_name.copy(ret->mod_type,string::npos);
     ret->mod_type[m_name.size()]=0;
-       ret->mod_bvalues=this->getBerValues();
-       return ret;
+    ret->mod_bvalues=this->getBerValues();
+    return ret;
 }
 
 bool LDAPAttribute::isNotPrintable() const {
-       StringList::const_iterator i;
-       for(i=m_values.begin(); i!=m_values.end(); i++){
-               size_t len = i->size();
-               for(size_t j=0; j<len; j++){
-                       if (! isprint( (i->data())[j] ) ){
-                               return true;
-                       }
-               }
+    StringList::const_iterator i;
+    for(i=m_values.begin(); i!=m_values.end(); i++){
+       size_t len = i->size();
+       for(size_t j=0; j<len; j++){
+           if (! isprint( (i->data())[j] ) ){
+               return true;
+           }
        }
-       return false;
+    }
+    return false;
 }
 
 ostream& operator << (ostream& s, const LDAPAttribute& attr){
-       s << attr.m_name << "=";
-       StringList::const_iterator i;
-       if (attr.isNotPrintable()){
-               s << "NOT_PRINTABLE" ;
-       }else{
-               for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
-                       s << *i << " ";
-               }
+    s << attr.m_name << "=";
+    StringList::const_iterator i;
+    if (attr.isNotPrintable()){
+           s << "NOT_PRINTABLE" ;
+    }else{
+       for(i=attr.m_values.begin(); i!=attr.m_values.end(); i++){
+           s << *i << " ";
        }
+    }
        return s;
 }
index 94881b70ecc942ec09a7adbc229bbdf228be56ae..7c26ef3ec5d3dbc814274f57b7c4e641d3d095c2 100644 (file)
@@ -92,7 +92,7 @@ void LDAPAttributeList::addAttribute(const LDAPAttribute& attr){
 
 LDAPMod** LDAPAttributeList::toLDAPModArray() const{
     DEBUG(LDAP_DEBUG_TRACE,"LDAPAttribute::toLDAPModArray()" << endl);
-    LDAPMod **ret = new LDAPMod*[m_attrs.size()+1];
+    LDAPMod **ret = (LDAPMod**) malloc((m_attrs.size()+1) * sizeof(LDAPMod*));
     AttrList::const_iterator i;
     int j=0;
     for (i=m_attrs.begin(); i!= m_attrs.end(); i++, j++){
index ca5b75a571d5ab9a716742be421ea4ec4b820891..28d0cf682196c7526a0d689eb87edbbdc12de184 100644 (file)
@@ -42,12 +42,12 @@ LDAPMessageQueue* LDAPBindRequest::sendRequest(){
     const char* mech = (m_mech == "" ? 0 : m_mech.c_str());
     BerValue* tmpcred=0;
     if(m_cred != ""){
-        char* tmppwd = new char[m_cred.size()+1];
+        char* tmppwd = (char*) malloc( (m_cred.size()+1) * sizeof(char));
         m_cred.copy(tmppwd,string::npos);
         tmppwd[m_cred.size()]=0;
         tmpcred=ber_bvstr(tmppwd);
     }else{
-        tmpcred=new BerValue;
+        tmpcred=(BerValue*) malloc(sizeof(BerValue));
         tmpcred->bv_len=0;
         tmpcred->bv_val=0;
     }
@@ -59,8 +59,8 @@ LDAPMessageQueue* LDAPBindRequest::sendRequest(){
     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
     int err=ldap_sasl_bind(m_connection->getSessionHandle(),dn, 
             mech, tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
     ber_bvfree(tmpcred);
 
     if(err != LDAP_SUCCESS){
index 7e6a731a944513fec9c033a6e93390148bcb3413..048c9d1e7b5be262b2a3d517fc78e2f79a50c9cc 100644 (file)
@@ -50,8 +50,8 @@ LDAPMessageQueue* LDAPCompareRequest::sendRequest(){
             m_attr.getName().c_str(), val[0], tmpSrvCtrls, 
             tmpClCtrls, &msgID);
     ber_bvecfree(val);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
     if(err != LDAP_SUCCESS){
         throw LDAPException(err);
     }else{
index 073191cfa569ccf5446bc2bc81821c41711ea6f5..6e7c94bb920811270de33659ade8f3d2d015facd 100644 (file)
@@ -79,3 +79,10 @@ LDAPControl* LDAPCtrl::getControlStruct() const {
     return ret;
 }
 
+void LDAPCtrl::freeLDAPControlStruct(LDAPControl *ctrl){
+    DEBUG(LDAP_DEBUG_TRACE,"LDAPCtrl::freeControlStruct()" << endl);
+    delete[] ctrl->ldctl_oid;
+    delete[] ctrl->ldctl_value.bv_val;
+    delete ctrl;
+}
+
index 38c5ce50b5d58f64658be97662b7ee3e31110cc9..bdec5c3bfbe883dcfd7b6fd7ea904f501c9b8e9c 100644 (file)
@@ -73,7 +73,8 @@ class LDAPCtrl{
          * the C-API
          */
         LDAPControl* getControlStruct() const;
-    
+       static void freeLDAPControlStruct(LDAPControl *ctrl);
+
     private :
         std::string m_oid;
         std::string m_data;
index 53d8496046ae59b9eb410639a8f8e36886e3e22e..9f62363c489672f1a10f741c3c0622ba068ddf22 100644 (file)
@@ -72,3 +72,12 @@ LDAPControl** LDAPControlSet::toLDAPControlArray() const{
     }
 }
 
+void LDAPControlSet::freeLDAPControlArray(LDAPControl **ctrl){
+    DEBUG(LDAP_DEBUG_TRACE, "LDAPControlSet::freeLDAPControlArray()" << endl);
+    if( ctrl ){
+        for( LDAPControl **i = ctrl; *i != 0; ++i ){
+           LDAPCtrl::freeLDAPControlStruct(*i);
+       }
+    }
+    delete[] ctrl;
+}
index 353d939e98e69e6bd17d0182a5796df30e10ee96..82b4649e29deddae48025effa05b15a0ad198b7d 100644 (file)
@@ -81,7 +81,7 @@ class LDAPControlSet {
          * LDAPControl-structures as needed by the C-API
          */
         LDAPControl** toLDAPControlArray()const ;
-
+       static void freeLDAPControlArray(LDAPControl **ctrl);
     private :
         CtrlList data;
 } ;
index bad6adcdfe5f45bac61041f9eaaf4e6c20f1c17d..e6fbb251b0f89bb392fdbeb4a35f3ec971e05e57 100644 (file)
@@ -47,8 +47,8 @@ LDAPMessageQueue* LDAPDeleteRequest::sendRequest(){
     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
     int err=ldap_delete_ext(m_connection->getSessionHandle(),m_dn.c_str(), 
             tmpSrvCtrls, tmpClCtrls ,&msgID);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
     if(err != LDAP_SUCCESS){
         throw LDAPException(err);
     }else{
index 325e2a4e50e7c2aede9179f345533e03743cd4d8..3af69cc593867b0d97dfbb6a0e2f1069ed1a7382 100644 (file)
@@ -30,7 +30,7 @@ LDAPEntry::LDAPEntry(const LDAPAsynConnection *ld, LDAPMessage *msg){
     DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPEntry::LDAPEntry()" << endl);
     char* tmp=ldap_get_dn(ld->getSessionHandle(),msg);
     m_dn=string(tmp);
-    delete[] tmp;
+    free(tmp);
     m_attrs = new LDAPAttributeList(ld, msg);
 }
 
index 18b0f42aeb6faf1fc78b849b0ff8080c977893d2..b7f54eeecd3b6899bb1347c2cbd3b4edd4396961 100644 (file)
@@ -41,17 +41,17 @@ LDAPMessageQueue* LDAPExtRequest::sendRequest(){
     int msgID=0;
     BerValue* tmpdata=0;
     if(m_data != ""){
-        tmpdata=new BerValue;
+        tmpdata=(BerValue*) malloc(sizeof(BerValue));
         tmpdata->bv_len = m_data.size();
-        tmpdata->bv_val = new char[m_data.size()];
+        tmpdata->bv_val = (char*) malloc(sizeof(char) * (m_data.size()) );
         m_data.copy(tmpdata->bv_val, string::npos);
     }
     LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
     int err=ldap_extended_operation(m_connection->getSessionHandle(),
             m_oid.c_str(), tmpdata, tmpSrvCtrls, tmpClCtrls, &msgID);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
     ber_bvfree(tmpdata);
     if(err != LDAP_SUCCESS){
         delete this;
index cf5bb0d805417e76fcd725d091b12e4932e94243..2668609e8768d861a28bf41d412e556ea23c97bd 100644 (file)
@@ -41,18 +41,18 @@ LDAPMessageQueue::~LDAPMessageQueue(){
 
 LDAPMsg *LDAPMessageQueue::getNext(){
     DEBUG(LDAP_DEBUG_TRACE,"LDAPMessageQueue::getNext()" << endl);
-       LDAPMessage *msg;
+    LDAPMessage *msg;
     LDAPRequest *req=m_activeReq.top();
     int msg_id = req->getMsgID();
-       int res;
+    int res;
     const  LDAPAsynConnection *con=req->getConnection();
     res=ldap_result(con->getSessionHandle(),msg_id,0,0,&msg);
     if (res <= 0){
         if(msg != 0){
             ldap_msgfree(msg);
         }
-               throw  LDAPException(con);
-       }else{  
+       throw  LDAPException(con);
+    }else{     
         const LDAPConstraints *constr=req->getConstraints();
         LDAPMsg *ret=0;
         //this can  throw an exception (Decoding Error)
@@ -144,7 +144,7 @@ LDAPMsg *LDAPMessageQueue::getNext(){
                 }
             break;
         }
-       }       
+    }  
 }
 
 // TODO Maybe moved to LDAPRequest::followReferral seems more reasonable
index 44f4aaadb7a9c8cc51226514f96fb6b6c1879ee2..0d964370791d47e1937d47ed5cd26ed8d46cb29e 100644 (file)
@@ -60,8 +60,8 @@ LDAPMessageQueue* LDAPModDNRequest::sendRequest(){
     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
     int err=ldap_rename(m_connection->getSessionHandle(),m_dn.c_str(),newRDN,
             newParentDN,m_deleteOld ? 1 : 0, tmpSrvCtrls, tmpClCtrls,&msg_id);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
     if(err!=LDAP_SUCCESS){
         throw LDAPException(err);
     }else{
index 5b0ae9b12e891456cf59f2e6c854ca86772ebbb6..d8bed4f6851d6b778f66aa25ade9513a923d9cac 100644 (file)
@@ -25,12 +25,13 @@ void LDAPModList::addModification(const LDAPModification &mod){
 
 LDAPMod** LDAPModList::toLDAPModArray(){
     DEBUG(LDAP_DEBUG_TRACE,"LDAPModList::toLDAPModArray()" << endl);
-       LDAPMod **ret = new LDAPMod*[m_modList.size()+1];
-       ret[m_modList.size()]=0;
-       ModList::const_iterator i;
-       int j=0;
-       for (i=m_modList.begin(); i != m_modList.end(); i++ , j++){
-               ret[j]=i->toLDAPMod();
-       }
-       return ret;
+    LDAPMod **ret = (LDAPMod**) malloc(
+                   (m_modList.size()+1) * sizeof(LDAPMod*));
+    ret[m_modList.size()]=0;
+    ModList::const_iterator i;
+    int j=0;
+    for (i=m_modList.begin(); i != m_modList.end(); i++ , j++){
+           ret[j]=i->toLDAPMod();
+    }
+    return ret;
 }
index cab3176f08a6fd60e68effd82ae39b220e008fb5..969385275367489e0fc755b974ef97de35d174ee 100644 (file)
@@ -13,27 +13,27 @@ LDAPModification::LDAPModification(const LDAPAttribute& attr, mod_op op){
     DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPModification::LDAPModification()" << endl);
     DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER,
             "   attr:" << attr << endl);
-       m_attr = attr;
-       m_mod_op = op;
+    m_attr = attr;
+    m_mod_op = op;
 }
 
 LDAPMod* LDAPModification::toLDAPMod() const  {
     DEBUG(LDAP_DEBUG_TRACE,"LDAPModification::toLDAPMod()" << endl);
-       LDAPMod* ret=m_attr.toLDAPMod();
+    LDAPMod* ret=m_attr.toLDAPMod();
 
-       //The mod_op value of the LDAPMod-struct needs to be ORed with the right
-       // LDAP_MOD_* constant to preserve the BIN-flag (see CAPI-draft for 
+    //The mod_op value of the LDAPMod-struct needs to be ORed with the right
+    // LDAP_MOD_* constant to preserve the BIN-flag (see CAPI-draft for 
     // explanation of the LDAPMod struct)
-       switch (m_mod_op){
-               case OP_ADD :
-                       ret->mod_op |= LDAP_MOD_ADD;
-               break;
-               case OP_DELETE :
-                       ret->mod_op |= LDAP_MOD_DELETE;
-               break;
-               case OP_REPLACE :
-                       ret->mod_op |= LDAP_MOD_REPLACE;
-               break;
-       }
-       return ret;
+    switch (m_mod_op){
+       case OP_ADD :
+           ret->mod_op |= LDAP_MOD_ADD;
+       break;
+       case OP_DELETE :
+           ret->mod_op |= LDAP_MOD_DELETE;
+       break;
+       case OP_REPLACE :
+           ret->mod_op |= LDAP_MOD_REPLACE;
+       break;
+    }
+    return ret;
 }
index c5f2fbf0987aa6a784465a33b75981bb26628d48..579ff8b74f3b23c5ddd073021b6f5b14c8a6ecac 100644 (file)
@@ -51,8 +51,9 @@ LDAPMessageQueue* LDAPModifyRequest::sendRequest(){
     LDAPMod** tmpMods=m_modList->toLDAPModArray();
     int err=ldap_modify_ext(m_connection->getSessionHandle(),m_dn.c_str(),
             tmpMods, tmpSrvCtrls, tmpClCtrls,&msgID);
-    ldap_controls_free(tmpSrvCtrls);
-    ldap_controls_free(tmpClCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
+    ldap_mods_free(tmpMods,1);
     if(err != LDAP_SUCCESS){
         throw LDAPException(err);
     }else{
index ea54d9bf2a25ed7fb35be4d534936618a472a44b..598adc330a3ea5e38a859e4d6ecce413d14134af 100644 (file)
@@ -41,11 +41,11 @@ LDAPResult::LDAPResult(const LDAPRequest *req, LDAPMessage *msg) :
             }
             if(matchedDN != 0){
                 m_matchedDN=string(matchedDN);
-                delete[] matchedDN;
+                free(matchedDN);
             }
             if(errMsg != 0){
                 m_errMsg=string(errMsg);
-                delete[] errMsg;
+                free(errMsg);
             }
         }
     }
index e1f758083d399819b76253ee14c34e206c237baa..aa13cd77c9959a45a88a583c973a594ed7535d7f 100644 (file)
@@ -71,8 +71,8 @@ LDAPMessageQueue* LDAPSearchRequest::sendRequest(){
             tmpClCtrl, tmptime, m_cons->getSizeLimit(), &msgID );
     delete tmptime;
     ldap_value_free(tmpattrs);
-    ldap_controls_free(tmpSrvCtrl);
-    ldap_controls_free(tmpClCtrl);
+    LDAPControlSet::freeLDAPControlArray(tmpSrvCtrl);
+    LDAPControlSet::freeLDAPControlArray(tmpClCtrl);
 
     if (err != LDAP_SUCCESS){  
         throw LDAPException(err);
index 632a20c822d8f89970271f368a701c7bbe3a2bb0..c997c42dfdf92e8545108c60d532ebee928ee805 100644 (file)
@@ -32,11 +32,11 @@ StringList::~StringList(){
 
 char** StringList::toCharArray() const{
     if(!empty()){
-        char** ret = new char*[size()+1];
+        char** ret = (char**) malloc(sizeof(char*) * (size()+1));
         StringList::const_iterator i;
         int j=0;
         for(i=begin(); i != end(); i++,j++){
-            ret[j]=new char[i->size()+1];
+            ret[j]=(char*) malloc(sizeof(char) * (i->size()+1));
             i->copy(ret[j],string::npos);
             ret[j][i->size()]=0;
         }