[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#162584: memory leak in gethostbyname_r



Package: libc6
Version: 2.2.5-14.3
Severity: normal


-- System Information:
Debian Release: testing/unstable
Architecture: i386
Kernel: Linux server 2.2.20 #5 Mon Jul 8 21:10:05 CEST 2002 i686
Locale: LANG=C, LC_CTYPE=de_DE@euro

Versions of packages libc6 depends on:
ii  libdb1-compat                 2.1.3-4    The Berkeley database routines [gl

-- no debconf information

I think I've found a memory leak in the function `gethostbyname_r'.

Example code:

#define _GNU_SOURCE
#include <netdb.h>
#include <pthread.h>

void *lookup(void *arg)
{
	char buf[8192];
	struct hostent ret,*res;
	int err;

	gethostbyname_r((char *)arg, &ret, buf, 8192, &res, &err);

	return NULL;
}

int main(void)
{
	pthread_t tid;

	do {
		pthread_create(&tid, NULL, lookup, (void *) "www.debian.org");
	} while (!pthread_join(tid, NULL));
	
	return 0;
}

I compile the code like this:
gcc -Wall -O2 -o bug bug.c -lpthread

I've found 3 ways to avoid the memory leak in this code:
- compile it with -static
- comment out gethostbyname_r()
- don't create a new thread before calling gethostbyname_r() ->
    for(;;) lookup("www.debian.org");

Every thread can leak memory only once, afaics.

Using dynamically allocated memory for `buf' and `ret' doesn't change
anything.

Regards
Joern Heissler





Reply to: