getpwent() routine in <pwd.h> and memory leak?

Bug #999922 reported by Ville Kesola
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
eglibc (Ubuntu)
New
Undecided
Unassigned

Bug Description

Hi,

How to reproduce error? while ((ent = getpwent()) != NULL)

Following c program causes memory leak:

#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>

int main (void) {
    struct passwd *ent = NULL;
    printf ("ent struct is %d bytes\n", sizeof(*ent));
    while ((ent = getpwent()) != NULL) {
        printf ("pointer is %p, user is %s\n", ent, ent->pw_name);
    }
    endpwent();
    return 0;
}

getpwent() memory leak has been checked with Valgrind :

vkesola@vkesola-laptop:~/development/c/apollo/SystemLibraries$ valgrind --leak-check=full ./a.out
==6429== Memcheck, a memory error detector
==6429== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6429== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==6429== Command: ./a.out
==6429==
ent struct is 28 bytes
pointer is 0x41a9c1c, user is root
pointer is 0x41a9c1c, user is daemon
pointer is 0x41a9c1c, user is bin
pointer is 0x41a9c1c, user is sys
pointer is 0x41a9c1c, user is sync
pointer is 0x41a9c1c, user is games
pointer is 0x41a9c1c, user is man
pointer is 0x41a9c1c, user is lp
pointer is 0x41a9c1c, user is mail
pointer is 0x41a9c1c, user is news
pointer is 0x41a9c1c, user is uucp
pointer is 0x41a9c1c, user is proxy
pointer is 0x41a9c1c, user is www-data
pointer is 0x41a9c1c, user is backup
pointer is 0x41a9c1c, user is list
pointer is 0x41a9c1c, user is irc
pointer is 0x41a9c1c, user is gnats
pointer is 0x41a9c1c, user is nobody
pointer is 0x41a9c1c, user is libuuid
pointer is 0x41a9c1c, user is syslog
pointer is 0x41a9c1c, user is messagebus
pointer is 0x41a9c1c, user is avahi-autoipd
pointer is 0x41a9c1c, user is avahi
pointer is 0x41a9c1c, user is usbmux
pointer is 0x41a9c1c, user is gdm
pointer is 0x41a9c1c, user is speech-dispatcher
pointer is 0x41a9c1c, user is kernoops
pointer is 0x41a9c1c, user is pulse
pointer is 0x41a9c1c, user is rtkit
pointer is 0x41a9c1c, user is hplip
pointer is 0x41a9c1c, user is saned
pointer is 0x41a9c1c, user is ubuntu
pointer is 0x41a9c1c, user is vkesola
pointer is 0x41a9c1c, user is postfix
pointer is 0x41a9c1c, user is postgres
pointer is 0x41a9c1c, user is smmta
pointer is 0x41a9c1c, user is smmsp
pointer is 0x41a9c1c, user is mysql
==6429==
==6429== HEAP SUMMARY:
==6429== in use at exit: 160 bytes in 11 blocks
==6429== total heap usage: 72 allocs, 61 frees, 7,138 bytes allocated
==6429==
==6429== 160 (40 direct, 120 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==6429== at 0x4026864: malloc (vg_replace_malloc.c:236)
==6429== by 0x412CCDB: nss_parse_service_list (nsswitch.c:626)
==6429== by 0x412D2B4: __nss_database_lookup (nsswitch.c:167)
==6429== by 0x402EE9B: ???
==6429== by 0x403056C: ???
==6429== by 0x412E086: __nss_getent_r (getnssent_r.c:171)
==6429== by 0x40E314C: getpwent_r@@GLIBC_2.1.2 (getXXent_r.c:162)
==6429== by 0x412DC5D: __nss_getent (getnssent.c:37)
==6429== by 0x40E2BE0: getpwent (getXXent.c:85)
==6429== by 0x804846F: main (in /home/vkesola/development/c/apollo/SystemLibraries/a.out)
==6429==
==6429== LEAK SUMMARY:
==6429== definitely lost: 40 bytes in 1 blocks
==6429== indirectly lost: 120 bytes in 10 blocks
==6429== possibly lost: 0 bytes in 0 blocks
==6429== still reachable: 0 bytes in 0 blocks
==6429== suppressed: 0 bytes in 0 blocks
==6429==
==6429== For counts of detected and suppressed errors, rerun with: -v
==6429== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 19 from 8)

What happened?
Leak summary shows that definitely lost: 40 bytes in 1 blocks and indirectly lost: 120 bytes in 10 blocks.

What I expect?
I expect that there is no memory leaks.

$ lsb_release -rd
Description: Ubuntu 11.04
Release: 11.04

$ dpkg -S /usr/include/pwd.h
libc6-dev: /usr/include/pwd.h

$ dpkg --list libc6-dev
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-==============-==============-============================================
ii libc6-dev 2.13-0ubuntu13 Embedded GNU C Library: Development Librarie

$ dpkg --list valgrind
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-==============-==============-============================================
ii valgrind 1:3.6.1-0ubunt A memory debugger and profiler

$ apt-cache policy libc6-dev
libc6-dev:
  Installed: 2.13-0ubuntu13.1
  Candidate: 2.13-0ubuntu13.1
  Version table:
 *** 2.13-0ubuntu13.1 0
        500 http://fi.archive.ubuntu.com/ubuntu/ natty-updates/main i386 Packages
        500 http://security.ubuntu.com/ubuntu/ natty-security/main i386 Packages
        100 /var/lib/dpkg/status
     2.13-0ubuntu13 0
        500 http://fi.archive.ubuntu.com/ubuntu/ natty/main i386 Packages

best regards,
Ville Kesola

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.