This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/5468] Error in getline(3) manpage
- From: "mtk.manpages at gmail dot com" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sourceware dot org
- Date: Tue, 07 Jan 2014 21:04:54 +0000
- Subject: [Bug libc/5468] Error in getline(3) manpage
- Auto-submitted: auto-generated
- References: <bug-5468-131 at http dot sourceware dot org/bugzilla/>
https://sourceware.org/bugzilla/show_bug.cgi?id=5468
--- Comment #3 from Michael Kerrisk <mtk.manpages at gmail dot com> ---
(In reply to Luke Hutchison from comment #0)
> The getline(3) manpage states:
>
> ssize_t getline(char **lineptr, size_t *n, FILE *stream);
> [...]
> If *lineptr is NULL, then getline() will allocate a buffer for
> storing the line, which should be
> freed by the user program. (The value in *n is ignored.)
>
> Based on this description (particularly the part that says the value *n is
> ignored), you should be able to do something like:
>
> char *line = NULL;
> getline(&line, NULL, stream);
>
> However this always fails, because the code for getline does the following:
>
> if (!lineptr || !n || !stream)
> {
> errno = EINVAL;
> return -1;
> }
>
> if (!*lineptr)
> {
> *n = MIN_CHUNK;
> *lineptr = malloc (*n);
> if (!*lineptr)
> {
> errno = ENOMEM;
> return -1;
> }
> *lineptr[0] = '\0';
> }
>
> (thus the !n check always causes -1 to be returned).
>
> Probably the manpage should just be modified to say something like "(The
> value
> in *n is ignored on entry, but *n must still point to a valid int, and its
> value
> is updated on exit to reflect the size of the newly malloc'd buffer.)"
>
> This is pretty confusing behavior otherwise -- I had to read the source for
> getline() to know what was going on.
>
> Thanks!
getline(3) text changed to read:
If *lineptr is set to NULL and *n is set 0 before the call,
then getline() will allocate a buffer for storing the line,
which should be freed by the user program.
--
You are receiving this mail because:
You are on the CC list for the bug.