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

Bug#318317: libc6: Numerous (49) memory leaks in gethostbyname, as reported by mudflap



Very simple: gethostbyname() returns a "struct hostent *" for which the
C library has to allocate some internal memory. However,
POSIX/SUS/etc. does not define any API to tell the C library that the
returned pointer is no longer needed, so the allocated memory cannot be
freed by the C library until the next call to gethostbyname().

Solution: do not use the gethostby* functions. Use get{addr|name}info()
instead, they do not have this API problem (and have other advantages
as well).

Florian and Gabor -- thanks! Gabor, I am using gethostbyname for portability reasons, since some systems may lack the other APIs, but it indeed would be good then to add a code path that uses the abovementioned functions, which I recently became aware of myself.

Finally -- when the program is:

#include <stdlib.h>
#include <netdb.h>

int main()
{
   stuct hostent* hi = gethostbyname(www.google.com);
   free(hi);
   return 0;
}

the number of leaks reported does not change (nor other errors appear) so it seems that it is not the hostent structure that causes this, but rather some other state that gethostbyname keeps. Note that mudflap prints the leak report at program termination.


Reply to: