Bugzilla will be upgraded to version 5.0 on a still to be determined date in the near future. The original upgrade date has been delayed.
Bug 101327 - getnameinfo leaks memory
getnameinfo leaks memory
Product: Red Hat Linux
Classification: Retired
Component: glibc (Show other bugs)
i686 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Brian Brock
Depends On:
  Show dependency treegraph
Reported: 2003-07-30 17:22 EDT by Mark G. Adams
Modified: 2016-11-24 10:23 EST (History)
2 users (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2003-10-02 05:41:34 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Mark G. Adams 2003-07-30 17:22:37 EDT
From Bugzilla Helper:
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225

Description of problem:
when running a program using getnameinfo with valgrind, valgrind reports a
memory leak as follows:
==26692== 22 bytes in 1 blocks are definitely lost in loss record 2 of 3
==26692==    at 0x4002910A: malloc (vg_replace_malloc.c:153)
==26692==    by 0x402DD2DF: __GI___strdup (in /lib/libc-2.3.2.so)
==26692==    by 0x413A28F9: ???
==26692==    by 0x4035A688: gethostbyaddr_r@@GLIBC_2.1.2 (in
/lib/libc-2.3.2.so)==26692==    by 0x403624BE: __GI_getnameinfo (in
==26692==    by 0x8048593: main (tester.c:28)
==26692==    by 0x40278A46: __libc_start_main (in /lib/libc-2.3.2.so)
==26692==    by 0x8048428: ??? (start.S:81)
This is run with valgrind-20030716.

Version-Release number of selected component (if applicable):

How reproducible:

Steps to Reproduce:
1. Compile below program
2. Use it to look up an IPv4 address with valgrind

Actual Results:  Memory leak reported in getnameinfo

Expected Results:  No memory leak

Additional info:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>

int main (int argc, char **argv)
   char             Buf[NI_MAXHOST];
   struct addrinfo *p_Addrs;
   int              ret;

   if (argc < 2)
      fprintf (stderr, "Syntax: gethostbyaddr <ip>\n");
      return 1;

   ret = getaddrinfo (argv[1], NULL, NULL, &p_Addrs);

   if (ret != 0)
      freeaddrinfo (p_Addrs);
      fprintf (stderr, "getaddrinfo failed: %s\n", gai_strerror (ret));
      return 1;

   ret = getnameinfo (p_Addrs->ai_addr, p_Addrs->ai_addrlen,
                      Buf, sizeof (Buf), NULL, 0, NI_NAMEREQD);
   freeaddrinfo (p_Addrs);

   if (ret != 0)
      fprintf (stderr, "getnameinfo failed: %s\n", gai_strerror (ret));
      return 1;

   printf ("Host: '%s'\n", Buf);
   return 0;
Comment 2 Ulrich Drepper 2003-10-02 05:41:34 EDT
This is not really a memory leak.  The memory is allocated once, and reused ever
after.  Never freed, never overwritten.  We do not free such memory on normal
application time termination time which is why you see this messages from valgrind.

I'll add some magic to the code which does free the string when a memory
debugger is used but this is not reason enough for an errata.  It might be in a
future bug fix release but not guaranteed since no harm is caused.

Note You need to log in before you can comment on or make changes to this bug.