Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is getnameinfo memory leaking confirmed?

As per question, I'm running into some memory leaking by getnameinfo. I'm using Ubuntu 12.04 (Linux scv 3.2.0-35-generic #55-Ubuntu SMP Wed Dec 5 17:42:16 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux) with gcc version 4.6.3.
I'm linking my server executable with g++ and so far valgrind hasn't reported an issue. I've then added a simple call to getnameinfo to print out what are the network name and port of connecting clients.
And I get the following:

==4425== 
==4425== HEAP SUMMARY:
==4425==     in use at exit: 10 bytes in 1 blocks
==4425==   total heap usage: 4,508 allocs, 4,507 frees, 134,939,153 bytes allocated
==4425== 
==4425== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4425==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4425==    by 0x50D7D71: strdup (strdup.c:43)
==4425==    by 0x1484B861: ???
==4425==    by 0x515B871: gethostbyaddr_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
==4425==    by 0x5161D06: getnameinfo (getnameinfo.c:223)
==4425==    by 0x404175: solsrv_run (solsrv.c:381)
==4425==    by 0x404DAC: main (main.c:167)
==4425== 
==4425== LEAK SUMMARY:
==4425==    definitely lost: 10 bytes in 1 blocks
==4425==    indirectly lost: 0 bytes in 0 blocks
==4425==      possibly lost: 0 bytes in 0 blocks
==4425==    still reachable: 0 bytes in 0 blocks
==4425==         suppressed: 0 bytes in 0 blocks
==4425== 
==4425== For counts of detected and suppressed errors, rerun with: -v
==4425== ERROR SUMMARY: 12 errors from 11 contexts (suppressed: 2 from 2)

What am I doing wrong?
Code is simply as follows:

struct sockaddr addr;
socklen_t           addr_sz = sizeof(addr);
char        host[NI_MAXHOST],
            serv[NI_MAXSERV];
int infd = accept(srv_fd, (struct sockaddr*)&addr, &addr_sz);
if (infd == -1) {
    ... manage error on accept ...
}
if(getnameinfo((struct sockaddr *)&addr, addr_sz, host, NI_MAXHOST, serv, NI_MAXSERV, NI_NUMERICSERV)) {
    strncpy(host, "<unknown host>", NI_MAXHOST-1);
    strncpy(serv, "<unknown port>", NI_MAXSERV-1);
}

And there you have the leak...
I can confirm, that the leak is happening: for 6 clients connected valgrind found 60 bytes leaked (I guess the clients were connecting from the same host so if it's related to host name the growth is linear as expected).
Any idea?

Cheers

like image 536
Emanuele Avatar asked Dec 24 '12 20:12

Emanuele


People also ask

Are memory leaks OK?

Memory is "leaking" out of the heap, never to be seen again. You allocate the memory, work with it until the program terminates. This is not a memory leak; it doesn't impair the program, and all the memory will be scavenged up automagically when the program terminates. Generally, you should avoid memory leaks.

When can you tell that a memory leak will occur?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

Do memory leaks cause permanent damage?

Memory leaks don't result in physical or permanent damage. Since it's a software issue, it will slow down the applications or even your whole system. However, a program taking up a lot of RAM space doesn't always mean its memory is leaking somewhere. The program you're using may really need that much space.

Do all programs have memory leaks?

The reality is that memory leaks can strike any application in any language. They're more common in older or “closer to the metal” languages like C or C++, sure. But all it takes is a visit to one poorly-optimized web page to discover that even a language like JavaScript can have problems with memory leaks.


1 Answers

Eventually found the real leak.

When connecting to the server socket use name.local instead of localhost and/or the fully qualified name.
getnameinfo() will then leak.

I can reproduce the bug on 12.04, 12.10 both x64 and x86.
If I connect specifying .local on the name it leaks.

Cheers

like image 108
Emanuele Avatar answered Sep 21 '22 15:09

Emanuele