[omniORB] mem leak in LibcWrapper::getaddrinfo

Hartmut Raschick hartmut.raschick at ke-elektronik.de
Mon Jun 2 09:40:14 BST 2003


Hi everybody,
while purifying our program under Solaris9 I saw a mem leak in the
above function. After inspecting the source code I found that the
buffer allocated for gethostbyname_r is indeed not freed. After also
inspecting IP4AddrInfo, I saw that there appears to be no need to
keep the buffer (carrying the info returned by gethostbyname_r), like
you (omni) once did in LibcWrapper::hostent_var.
So the code IHMO should look like this (note that with other platform-
dependent later on in the method it's the same)

Every changed/new line preceeded by ">>>" (in line above)
I tried to stick to your formatting style.


file: <omni>/src/lib/omniORB2/orbcore/libcWrapper.cc

--snip----------------------------------------------------------
.
.
.
// Description:
//      Wrapper for libc functions which are non-reentrant
//

/*
  $Log: libcWrapper.cc,v $
  Revision 1.19.2.10  2003/02/17 02:03:08  dgrisby
  vxWorks port. (Thanks Michael Sturm / Acterna Eningen GmbH).

  Revision 1.19.2.9  2003/01/06 11:11:55  dgrisby
  New AddrInfo instead of gethostbyname.
.
.
.
LibcWrapper::AddrInfo* LibcWrapper::getaddrinfo(const char* node,
                                                CORBA::UShort port)
{

.
.
.

#if defined(__sunos__) && __OSVERSION__ >= 5

  // Use gethostbyname_r() on Solaris 2

  struct hostent ent;
  char* buffer = new char[256];
  int buflen = 256;
  int rc;
>>> have a pointer ready
  IP4AddrInfo *ip4;

again:
  if (gethostbyname_r(node,&ent,buffer,buflen,&rc) == 0) {
    if (errno == ERANGE) {
      // buffer is too small to store the result, try again
      delete [] buffer;
      buflen = buflen * 2;
      buffer = new char [buflen];
      goto again;
    }
    else
    {
>>> free it here also
      delete [] buffer;
      return 0;
    }
  }
>>> construct ret value
  ip4 = IP4AddrInfo(hostent_to_ip4(&ent), port);
>>> now able to free buffer
  delete [] buffer;
>>> return now
  return ip4;

#elif defined(__osf1__)

...same story here... and further on - hpux (watch out for "buffer")

.
.
.
}
--snap----------------------------------------------------------

P.S.: As far as the welfare of every other living form on earth was
      concerned, the human project was not just a failure, it was a
      mistake from the very beginning.
       - Ian McEwan, Amsterdam, 1998 booker prize

-- 
Hartmut "Hardy" Raschick / Dept. RD41
ke Kommunikations-Elektronik GmbH
Wohlenberstr. 3, 30179 Hannover
Phone: ++49 (0)511 6747-564
Fax: ++49 (0)511 6747-340
e-Mail: hartmut.raschick at ke-elektronik.de
http://www.ke-elektronik.de



More information about the omniORB-list mailing list