[163] in Hesiod

home help back first fref pref prev next nref lref last post

Re: Major memory leak in hes_resolv

daemon@ATHENA.MIT.EDU (Jeff Kutcher)
Tue Oct 12 17:15:17 1993

Date: Tue, 12 Oct 93 16:11 CDT
From: kutcher@nisus.eng.hou.compaq.com (Jeff Kutcher)
To: tytso@MIT.EDU
Cc: hesiod@MIT.EDU, kutcher@nisus.MIT.EDU, mcb@mach.eng.hou.compaq.com

> It is the program's responsibility to free the data structures returned
> by hes_resolve.  In this fashion, hes_resolve is more like strdup() in
> that it returns a pointer to allocated memory, than gethostbyname()
> which returns a pointer static memory.
> 
> So it's not a bug, but an issue of misunderstanding the programming
> interface to hes_resolve().
> 
> It's actually a feature, since if a program nees to call hes_resolve
> multiple times to get various different pieces of information, it
> doesn't need to copy the returned information to a another variable
> before calling hes_resolve() again.  Contrast this with the behavior of
> gethostbyname(), where many a bug has been called by having a procedure
> call gethostbyname(), and having it try to use the resulting information
> both before and after calling another function which also happens to
> call gethostbyname() for another purpose, unbeknownst to the author of
> the first procedure....
> 
>                                                 - Ted

In general, any system library call that returns a static
structure and calls hes_resolve(3) will have a memory leak if successive
calls are made to the library function. For example, hes_resolve returns
a pointer to an array of character pointers (char **) which is used by
getservbyname(3) to load a static servent structure. The contents of
the structure is the dynamic memory allocated (calloc) by hes_resolve.
Subsequent calls to getservbyname result in the lost reference to the contents
of previous allocated memory resulting in a memory leak. The resolution to
this problem is to modify the system library functions that use hes_resolve
(not hes_resolve). These functions, which return static structures, should
maintain a static reference to previously allocated store gotten from
a previous call to hes_resolve. Successive calls to these library functions
should free previous allocated memory before the next call to hes_resolve.

Jeff

-----------------------------------------------------------------------------

struct servent *
hes_getservent( name, service, proto )
char *name ;
char *service ;
char *proto ;
{
        char **hes_resolve() ;
        register char *l ;
        register char **cp ;
        static char **savecp = (char **)NULL;

        if(savecp)
                for(cp = savecp; *cp; cp++)
                        free(*cp);

        if ( NULL == ( savecp = cp = hes_resolve( name, service ) ) ) {
                return NULL ;
        }

	.
	.
	.

home help back first fref pref prev next nref lref last post