OpenLdap 에서 Memory Leak 을 못잡겠습니다.

litdream의 이미지

14 바이트가 해제가 안된다고 하네요.
메모리를 사용하고 나서는 항상 해제했다고 생각했는데,
지금 뜻대로 안되고 있습니다.

아무래도 제가 아직 LDAP 을 다루는데 많이 미숙한 모양입니다.
한수 부탁드릴수 있을까요?

$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes srch username password

==31849== Memcheck, a memory error detector for x86-linux.
==31849== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward et al.
==31849== Using valgrind-2.2.0, a program supervision framework for x86-linux.
==31849== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward et al.
==31849== For more details, rerun with: -v
==31849==    
==31849== Conditional jump or move depends on uninitialised value(s)
==31849==    at 0x1B8EC020: (within /lib/ld-2.3.4.so)
==31849==    by 0x1B8E5B6F: (within /lib/ld-2.3.4.so)
==31849==    by 0x1B8F0C5B: (within /lib/ld-2.3.4.so)
==31849==    by 0x1B8E4BAA: (within /lib/ld-2.3.4.so)
....
...<중간생략>
....
==31849== ERROR SUMMARY: 29 errors from 9 contexts (suppressed: 0 from 0)
==31849== malloc/free: in use at exit: 14 bytes in 1 blocks.
==31849== malloc/free: 212 allocs, 211 frees, 28888 bytes allocated.
==31849== For counts of detected errors, rerun with: -v
==31849== searching for pointers to 1 not-freed blocks.
==31849== checked 2843568 bytes.
==31849==
==31849==
==31849== 14 bytes in 1 blocks are still reachable in loss record 1 of 1
==31849==    at 0x1B902D4C: malloc (vg_replace_malloc.c:131)
==31849==    by 0x1BA6974E: ber_memalloc (in /usr/lib/liblber.so.2.0.130)
==31849==
==31849== LEAK SUMMARY:
==31849==    definitely lost: 0 bytes in 0 blocks.
==31849==    possibly lost:   0 bytes in 0 blocks.
==31849==    still reachable: 14 bytes in 1 blocks.
==31849==         suppressed: 0 bytes in 0 blocks.

#include <stdio.h>
#include <ldap.h>
#include <lber.h>

#define MYLDP_MAX_RESULT   120

//GLOBAL
char *host = "ldaptest.myorg.org";
int   desired_version = LDAP_VERSION3;
char *root_dn = "uid=username,dc=myorg,dc=org";
char *root_pw = "*secret*";
int auth_method = LDAP_AUTH_SIMPLE; 
char *base      = "dc=people,dc=ldap,dc=myorg,dc=org";

void
print_result(LDAP *ld, LDAPMessage *msg)
{
   LDAPMessage *entry; 
   BerElement *ber;

   // SEARCH VAR 
   char *dn, *attr, **vals;
   int i, result;

   for (entry = ldap_first_entry (ld, msg);
        entry != NULL; entry = ldap_next_entry (ld, entry))
   {
      if ((dn = ldap_get_dn (ld, entry)) != NULL)
      {
	 printf ("Returned dn: %s\n", dn);
	 ldap_memfree (dn);
      }
      
      for (attr = ldap_first_attribute (ld, entry, &ber);
	   attr != NULL; attr = ldap_next_attribute (ld, entry, ber))
      {
	 if ((vals = ldap_get_values (ld, entry, attr)) != NULL)
	 {
	    for (i = 0; vals[i] != NULL; i++)
	       printf ("%s: %s\n", attr, vals[i]);
	    ldap_value_free (vals);
	 }
	 ldap_memfree (attr);
      }
      if (ber != NULL)
	 ber_free (ber, 0);
      printf ("\n");
   }
   ldap_msgfree (msg);
}


void
default_search(LDAP *ld, LDAPMessage *msg, char *rtn, char empnum[])
{
   LDAPMessage *entry; 
   //BerElement *ber;

   /* SEARCH VAR */
   char *dn, *attr, **vals;
   int i, result;
   
   if ( (entry=ldap_first_entry(ld,msg)) != NULL ) {
      
      vals=ldap_get_values(ld,entry,"umid");
      strcat(rtn,vals[0]);
      strcat(rtn,",");
      ldap_value_free (vals);

      vals=ldap_get_values(ld,entry,"givenname");
      strcat(rtn,vals[0]);
      strcat(rtn,",");
      ldap_value_free (vals);

      vals=ldap_get_values(ld,entry,"sn");
      strcat(rtn,vals[0]);
      ldap_value_free (vals);

      vals=ldap_get_values(ld,entry,"employeenumber");
      strcpy(empnum,vals[0]);
      ldap_value_free (vals);
   }
   else {
      strcat(rtn,"NULL");
   }
}


int
default_auth(LDAP *ld, char *empnum, char *pswd) 
{
   char ldapid[100];

   snprintf(ldapid, 99, "employeenumber=%s,dc=people,dc=ldap,dc=umd,dc=edu",
            empnum);
   
   if (ldap_simple_bind_s(ld, ldapid, pswd) != LDAP_SUCCESS) {
      //TEST
      //printf("uid:%s  ||   %s\n", ldapid, pswd);
      //ldap_perror (ld, "ldap_user_auth");

      return -1;
   }
   return 0;
}

int
my_ldap_search(char *rtn, 
               char *ldapid, 
               char *ldappasswd)
{
   LDAP *ld;
   LDAPMessage *msg;
   char filter[100];
   char empnum[100];
   int result;
   
   if ( (ld = ldap_init(host,LDAP_PORT)) == NULL ) {
      perror ("ldap_init failed");
      return -1;
   }
   
   if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) !=
       LDAP_OPT_SUCCESS)
   {
      ldap_perror (ld, "ldap_set_option");
      return -1;
   }
   
   if (ldap_bind_s (ld, root_dn, root_pw, auth_method) != LDAP_SUCCESS)
   {
      ldap_perror (ld, "ldap_bind");
      return -1;
   }
   
   filter[0]='\0';
   strcat(filter,"uid=");
   strcat(filter,ldapid);
   
   if (ldap_search_s (ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &msg)
       != LDAP_SUCCESS)
   {
      ldap_perror (ld, "ldap_search_s");
      free(filter);
      return -1;
   }
   
   /***
       printf ("the number of entries returned was %d\n\n",
       ldap_count_entries (ld, msg));

       //print_result(ld, msg);
   ***/
   
   default_search(ld, msg, rtn, empnum);
   if (default_auth(ld, empnum, ldappasswd) == -1) 
      strcpy(rtn,"NULL");
   
   ldap_msgfree(msg);

   if ( (result = ldap_unbind_s(ld)) != 0)
   {
      fprintf (stderr, "ldap_unbind_s: (%d) %s\n", 
               result, ldap_err2string (result));
      exit (1);
   }

   return 0;
}


int 
main(int argc, char **argv)
{
   char *rtn;

   if (argc != 3) {
      printf("USAGE: srch <ldapid> <password>\n");
      exit(1);
   }
   
   rtn = (char*)malloc(MYLDP_MAX_RESULT +1);
   rtn[0] = '\0';

   if ( my_ldap_search(rtn, argv[1], argv[2]) < 0 ) 
      printf("USAGE: NULL\n");
   else
      printf("%s\n", rtn);

   free(rtn);
   return 0;
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.