Subject: Re: cygwin 1.3.17-1 gethostbyname() leak
From: "Noel Gordon" <[email-masked]>
To: <[email-masked]>
Cc: <[email-masked]>
Date: Wed, 11 Dec 2002 06:12:21 +1100

> On Tue, 10 Dec 2002, Noel Gordon wrote:
>
> > Anyone else confirm that the following program compiled under
> > gcc 3.2, cygwin 1.3.17-1, on Win2k leaks memory?
> >
> > /*-----------------------------------------------------------*/
> >
> > #include <netdb.h>
> > #include <stdio.h>
> >
> > int main()
> > {
> >   struct hostent *host;
> >
> >   while ( 1 )
> >   {
> >     if ( ( host = gethostbyname( "localhost" ) ) != NULL )
> >       printf( "gethostbyname(): %s\n", host->h_name );
> >   }
> > }

On Wednesday, December 11, Igor Pechtchanski wrote

> Noel,
>
> From http://www.opengroup.org/onlinepubs/007904975/functions/gethostbyaddr.html :
>
>       The gethostbyaddr() and gethostbyname() functions *may* return
>       pointers to static data, which may be overwritten by subsequent
>       calls to any of these functions.
>
> The "may" above (emphasis is mine) means that the implementation is free
> to malloc a data structure and require the user to free it.  Since you
> don't free the returned data structure in your program, it's quite
> possible that there's a leak.  Also, Cygwin uses the gethostbyname from
> wsock32.dll, which may have different implementations on different
> systems.  Thus, your best bet is to look up gethostbyname on MSDN for
> implementation details.
>
> Igor

If it is true that cygwin uses gethostbyname() from wsock32.dll, and
returns the wsock32.dll hostent* result to the application, then it
is perhaps unwise to free() it. Following your tip, we find from ...

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/gethostby
name_2.asp

  Remarks

  The gethostbyname function returns a pointer to a hostent structure - a
  structure allocated by Windows Sockets. The hostent structure contains
  the results of a successful search for the host specified in the name
  parameter [of gethostbyname()].

  The application must never attempt to modify this structure or to free
  any of its components. Furthermore, only one copy of this structure is
  allocated per thread, so the application should copy any information it
  needs before issuing any other Windows Sockets function calls.

...so there's seems to be more to it, but we can try free() approach for
fun, and quickly determine that doesn't help either. Ok, next stop is to
rip my code into test.c and compile with a "little" elaboration (-v) ...

noel@NOELG ~/test
$ gcc -v -include /usr/include/netdb.h -o test test.c
Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.2/specs
Configured with: /netrel/src/gcc-3.2-2/configure --enable-languages=c,c++,f77,ja
[*snip*]
 /usr/lib/gcc-lib/i686-pc-cygwin/3.2/collect2.exe -Bdynamic --dll-search-prefix=
cyg -o test.exe /usr/lib/gcc-lib/i686-pc-cygwin/3.2/../../../crt0.o /usr/lib/gcc
-lib/i686-pc-cygwin/3.2/crtbegin.o -L/usr/lib/gcc-lib/i686-pc-cygwin/3.2 -L/usr/
lib/gcc-lib/i686-pc-cygwin/3.2/../../.. /cygdrive/c/WINNT/TEMP/ccTCpBgj.o -lgcc
-lcygwin -luser32 -lkernel32 -ladvapi32 -lshell32 -lgcc /usr/lib/gcc-lib/i686-pc
-cygwin/3.2/crtend.o

so we can see which -l libraries are linked against my test program during the
final (ie., collect2) compilation stage. And the first of these -l's to define
gethostbyname() is the -lcygwin library ...

noel@NOELG ~/test
$ nm /usr/lib/libcygwin.a | grep gethostbyname
00000000 I __imp__gethostbyname
00000000 T _gethostbyname

Next to the implementation, which lives some in cygwin*.dll, so over to CVS to
find the component for gethostbyname(), and low and behold -- Corrina has been
diligently beavering away while we're just talking, and has updated the source
src/winsup/cygwin/net.cc to version 1.133, with a CVS log message

 * net.cc (free_protoent_ptr): Add missing free() for base structure.
 (free_servent_pt): Ditto.
 (free_hostent_pt): Ditto.

Thanks all, and well done Corrina, case closed. If private organisations fixed
issues this fast, the world might just become a better, less-buggy place.

-noel

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/