getline.3: Caller should free the allocated buffer even if getline() failed

Relevant discussion in glibc bugzilla:
https://sourceware.org/bugzilla/show_bug.cgi?id=5666
test program:

    $ cat tmp/getline/a.cc
    #include <stdio.h>
    int main() {
      for (int x = 0; x < 2; x++) {
        char *lineptr = 0;
        size_t size = 0;
        int res = getline(&lineptr, &size, stdin);
        printf("%d\n%p\n", res, lineptr);
      }
      return 0;
    }
    $ gcc tmp/getline/a.cc && echo -n "xxx" > x && ./a.out < x
    3
    0x11ae010
    -1
    0x11ae090

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Alexey Samsonov 2014-04-02 12:26:44 +04:00 committed by Michael Kerrisk
parent f3ef455821
commit cc82e457b2
1 changed files with 5 additions and 2 deletions

View File

@ -69,8 +69,11 @@ is set to NULL and
.I *n
is set 0 before the call, then
.BR getline ()
will allocate a buffer for storing the line,
which should be freed by the user program.
will allocate a buffer for storing the line.
This buffer should be freed by the user program
even if
.BR getline ()
failed.
Alternatively, before calling
.BR getline (),