From nobody@FreeBSD.org  Sat Jun 26 23:19:00 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 8CCB0106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 26 Jun 2010 23:19:00 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 628298FC14
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 26 Jun 2010 23:19:00 +0000 (UTC)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o5QNIx3Y048281
	for <freebsd-gnats-submit@FreeBSD.org>; Sat, 26 Jun 2010 23:18:59 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.3/8.14.3/Submit) id o5QNIx2J048280;
	Sat, 26 Jun 2010 23:18:59 GMT
	(envelope-from nobody)
Message-Id: <201006262318.o5QNIx2J048280@www.freebsd.org>
Date: Sat, 26 Jun 2010 23:18:59 GMT
From: Tiago Natel de Moura (I4K) <tiago4orion@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Possible Memory Leak in getaddrinfo()
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         148179
>Category:       kern
>Synopsis:       [libc] Possible Memory Leak in getaddrinfo(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jun 27 03:50:05 UTC 2010
>Closed-Date:    
>Last-Modified:  Sun Jun 27 04:09:35 UTC 2010
>Originator:     Tiago Natel de Moura (I4K)
>Release:        FreeBSD 8.0-RELEASE-p3 i386
>Organization:
BugSec
>Environment:
FreeBSD localhost.localdomain 8.0-RELEASE-p3 FreeBSD 8.0-RELEASE-p3 #0: Wed May 26 05:45:12 UTC 2010     root@i386-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
The function freeaddrinfo NOT release all the memory allocated by getaddrinfo.

I encountered this problem analyzing my code with Valgrind and it shows that after calling freeaddrinfo memory is still allocated.

I compiled and ran the same code on Linux (fedora and ubuntu) and they seem all right, no memory allocated at the end.

Output of the valgrind in FreeBSD:

--- snip ---
==16802== HEAP SUMMARY:
==16802==     in use at exit: 4,162 bytes in 6 blocks
==16802==   total heap usage: 67 allocs, 61 frees, 231,419 bytes allocated
==16802== 
==16802== Searching for pointers to 6 not-freed blocks
==16802== Checked 219,908 bytes
==16802== 
==16802== LEAK SUMMARY:
==16802==    definitely lost: 0 bytes in 0 blocks
==16802==    indirectly lost: 0 bytes in 0 blocks
==16802==      possibly lost: 0 bytes in 0 blocks
==16802==    still reachable: 4,162 bytes in 6 blocks
==16802==         suppressed: 0 bytes in 0 blocks
==16802== Rerun with --leak-check=full to see details of leaked memory
--- snip ---

Now, output on LINUX:

--- snip --
==5532== HEAP SUMMARY:
==5532==     in use at exit: 0 bytes in 0 blocks
==5532==   total heap usage: 86 allocs, 86 frees, 9,315 bytes allocated
==5532==
==5532== All heap blocks were freed -- no leaks are possible
==5532==
==5532== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 10)
--5532--
--5532-- used_suppression:     21 dl-hack3-cond-1
--- snip ---

I thought that the suppression "dl-hack3-cond-1" who could not be implemented in the ports version of valgrind but it has existed since the 3.3 version and my version is 3.5...


>How-To-Repeat:
Run the code below:

/*****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
 
int main()
{
    struct addrinfo hints, *res;
    int errcode;
 
    
    memset(&hints, 0, sizeof(hints));
    hints.ai_family=PF_UNSPEC;
    hints.ai_socktype=SOCK_STREAM;

    errcode=getaddrinfo("www.google.com", "http", &hints, &res);

    if (errcode)
    {
         printf("%s\n", gai_strerror(errcode));
         exit(1);
    }

    freeaddrinfo(res);
    
    return 0;
}

/*****************************************************/

$ valgrind -v ./addrinfo_leak

gcc (GCC) 4.2.1 20070719  [FreeBSD]
valgrind-3.5.0

>Fix:


>Release-Note:
>Audit-Trail:
>Unformatted:
