3

I have the following code, and for some reason valgrind finds some memory leaks in the getlogin() function. The code:

#include<unistd.h>
#include<stdio.h>

int main()
{
    char *userName = getlogin();
    printf("%s\n",userName);

    return 0;
}

The valgrind command which I use: valgrind --leak-check=full -v ./

The error I get:

 HEAP SUMMARY:
==2405==     in use at exit: 300 bytes in 11 blocks
==2405==   total heap usage: 67 allocs, 56 frees, 9,106 bytes allocated
==2405== 
==2405== Searching for pointers to 11 not-freed blocks
==2405== Checked 72,264 bytes
==2405== 
==2405== 300 (60 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==2405==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2405==    by 0x4F37CD4: nss_parse_service_list (nsswitch.c:678)
==2405==    by 0x4F38795: __nss_database_lookup (nsswitch.c:175)
==2405==    by 0x55F6623: ???
==2405==    by 0x4EF144C: getpwuid_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256)
==2405==    by 0x4F145AE: __getlogin_r_loginuid (getlogin_r.c:68)
==2405==    by 0x4F14304: getlogin (getlogin.c:35)
==2405==    by 0x400550: main (tmp1.c:6)
==2405== 
==2405== LEAK SUMMARY:
==2405==    definitely lost: 60 bytes in 1 blocks
==2405==    indirectly lost: 240 bytes in 10 blocks
==2405==      possibly lost: 0 bytes in 0 blocks
==2405==    still reachable: 0 bytes in 0 blocks
==2405==         suppressed: 0 bytes in 0 blocks
==2405== 
==2405== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
--2405-- 
--2405-- used_suppression:      2 dl-hack3-cond-1
==2405== 
==2405== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

Why does it happen? And what can I do to free it? Is there any other option to get the user name? Thanks in advance.

  • Try `free`ing `username` or better before that, read the documentation of `getlogin()`. – 0xF1 Apr 08 '14 at 09:10
  • @MadHatter `username` is defined in context of `main()`, not `getLogin()`, i doubt it is the reason – mangusta Apr 08 '14 at 09:11
  • 1
    @mangusta: Ya, I agree with u... But I think the address returned by `getlogin()` may be of dynamically allocated memory. – 0xF1 Apr 08 '14 at 09:13
  • 3
    @MadHatter The documentation states the string is statically allocated. Meaning you have no business of calling free() on it. What is the case here is a possible memory leak in nss_parse_service_list() in glibc - though probably a false positive. There's nothing to do about it, possibly add it to valgrind's ignore list. – nos Apr 08 '14 at 09:16
  • @madHatter I tried freeing, but then I get an error for freeing something that was not malloced. The program won't even run this way. – user3478330 Apr 08 '14 at 09:18
  • @nos: went through the documentation, you are right. Wherever it is happening, I think it is not the fault of user, it is better to ignore it. Or OP can try some different function/method. – 0xF1 Apr 08 '14 at 09:18
  • 1
    @nos: If it is `nss` then [this](http://stackoverflow.com/questions/1447018/getpwnam-r-memory-leak) may help. – 0xF1 Apr 08 '14 at 09:20

1 Answers1

0

With valgrind 3.10.0 I have no error

==27656== 
==27656== HEAP SUMMARY:
==27656==     in use at exit: 0 bytes in 0 blocks
==27656==   total heap usage: 67 allocs, 67 frees, 9,101 bytes allocated
==27656== 
==27656== All heap blocks were freed -- no leaks are possible
==27656== 
==27656== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==27656== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)>

Try to upgrade it or call __libc_freeres() after getlogin() But there is no error in your programme, this type of memory leak is acceptable.

Gnukos
  • 115
  • 1
  • 6