Bug 5468 - Error in getline(3) manpage
Summary: Error in getline(3) manpage
Status: RESOLVED DUPLICATE of bug 1481
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-12-10 22:42 UTC by Luke Hutchison
Modified: 2014-07-03 11:51 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Luke Hutchison 2007-12-10 22:42:00 UTC
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!
Comment 1 Ulrich Drepper 2007-12-10 22:52:42 UTC
glibc does not come with man pages.
Comment 2 Michael Kerrisk 2014-01-07 21:02:11 UTC

*** This bug has been marked as a duplicate of bug 1481 ***
Comment 3 Michael Kerrisk 2014-01-07 21:04:54 UTC
(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.