[Date Prev][Date Next] [Chronological] [Thread] [Top]

to free or to free thats the question



Hi all together!

the last days, I write a extention for a library to get some attributes
from the ldap server.
But the man pages and the openldap examples are differentially. The man
pages wrote I must free the ber element and attributes. If I do this the
the program end with signal 11 (SegV). If I do not this, it runs with
out problems und I have a mem leak in the theory.

Could anyone say me thats my problem?

Thanks
  Carsten

Here is the code:


  int         debug        = 0;

  /* this attribute contains the password value */
  const char  *ldapPWAttr  = NULL;

  int         searchScope  = LDAP_SCOPE_ONELEVEL;

  LDAP        *ld          = NULL;
  LDAPMessage *result      = NULL;
  LDAPMessage *entry       = NULL;
  BerElement  *ber         = NULL;
  
  const char  *attrs[3];
  char        filter[512];

  int         noRecords    = 0;
  /* get only attributes -> 1 or get attributes and values -> 0 */
  int         attrsonly    = 1;
  char        *attribute;
  char        **values;
  /* contains the value of the ldap attribute ldapPWAttr */
 
  
  /* generate a filter that will return the entry with the matching UID
*/
  sprintf(filter,ldapFilter,ldapUIDAttr, userid);

  attrs[0]  = ldapPWAttr;
  attrs[1]  = NULL;
  attrsonly = 0;                      // we need both attributes and
values


  /* perform the search */
  if (ldap_search_s(ld,ldapBaseDN,searchScope, filter,
      (char**) attrs, attrsonly, &result) != LDAP_SUCCESS) {
    syslog(LOG_DEBUG,"User %s not found",userid);
    goto finish_unbind;
  }
  
  /* if the entry count is not equal to one, either the UID was not
unique
     or there was no match */
  noRecords = ldap_count_entries(ld, result);
  if (noRecords != 1) {
    goto finish;
  }
  

  /* get the first entry */
  if ((entry = ldap_first_entry(ld,result)) == NULL) {
    syslog(LOG_ERR,"Can't get entry from LDAP / internal error,
userid=%s",
                userid);
    goto finish;
  }


  for ( attribute = ldap_first_attribute(ld, entry, &ber);
        attribute != NULL;
        attribute = ldap_next_attribute(ld, entry, ber) ) {

    values = ldap_get_values(ld, entry, attribute);
    
    /* make something */

    if (values)    { ldap_value_free(values); }

/* THATS MY PROBLEM:
   I don't understand this, the man page wrote the caller should freeing
the 
   attributes element. But if I free it, I get an segv. So I don't free
it.
   if (attribute) { ldap_memfree(attribute); } 
*/     
    }                                  // end for ( attribute ...
    /* get the results free */
/* THATS MY PROBLEM:
   I don't understand this, the man page wrote the caller should freeing
the 
   ber element. But if I free it, I get an segv. So I don't free it.
   if (ber)  { ber_free(ber, 0); }
*/    


  /* okay, we got it :-) */
finish:
    ldap_msgfree(result);
finish_unbind:
    ldap_unbind(ld);