This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/5666] New: when getline fails it still allocates a buffer
- From: "werner_hahn at yahoo dot de" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 24 Jan 2008 20:59:23 -0000
- Subject: [Bug libc/5666] New: when getline fails it still allocates a buffer
- Reply-to: sourceware-bugzilla at sourceware dot org
This may be a documentation issue.
When passing the getline function a NULL pointer as a buffer it is so kind to
allocate the needed memory. However when you reach for example EOF and the
function returns -1 a buffer of length 120 bytes is allocated anyway which must
be freed manually or else is lost. It took me quite some time to find out why
valgrind always told me of 120 lost bytes. The documentation does not mention
this and so I never suspected this function to be the memory leak.
As getline just functions as a wrapper around getdelim the issue should apply
to that as well.
I give a quick example:
char *line = NULL;
unsigned int count = 0;
count = getline(&line, &count, file_descriptor);
if(count == -1) // now line points to 120 bytes of junk
{
free(line); // without this free the buffer pointed to by line is lost
return NULL;
}
--
Summary: when getline fails it still allocates a buffer
Product: glibc
Version: 2.3.6
Status: NEW
Severity: normal
Priority: P2
Component: libc
AssignedTo: drepper at redhat dot com
ReportedBy: werner_hahn at yahoo dot de
CC: glibc-bugs at sources dot redhat dot com
http://sourceware.org/bugzilla/show_bug.cgi?id=5666
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.