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.