OpenLdap 에서 Memory Leak 을 못잡겠습니다.
글쓴이: litdream / 작성시간: 월, 2005/02/14 - 10:56오후
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;
}
Forums:


댓글 달기