Mr. Llama
Mr. Llama

Reputation: 20909

POSIX getline() - line buffer state on EOF?

This question is in regards to the POSIX C function getline.

The documentation states that getline returns -1 on error (including EOF), but it doesn't say what becomes of lineptr or n in those situations.
I understand that some errors may be handled differently - such as a failed realloc - but what about EOF? Do lineptr and n still retain their original values? Is it implementation specific? Undefined behavior?

Upvotes: 4

Views: 1558

Answers (3)

George Koehler
George Koehler

Reputation: 1703

The values of lineptr and n are implementation-specific. The getline() function might or might not reallocate the buffer when it reads EOF.

I point to Illumos, NetBSD, and FreeBSD as examples. I look at getdelim() because all 3 systems have getline() call getdelim() with delimiter '\n'.

  • Illumos getdelim() always tries for a buffer of at least 128 bytes, before it calls __filbuf to read the file. So, if you pass in *lineptr = NULL and it reads EOF, you get back *n = 128 and *lineptr pointing to 128 bytes of garbage.
  • NetBSD getdelim() always calls __srefill to read the file, before it tries any reallocation. If it reads EOF, you get back the same *lineptr that you passed in. If you passed in *lineptr = NULL, NetBSD does set *n = 0.
  • FreeBSD getdelim() also reads before reallocating, but if it reads EOF, then it puts a single '\0' (for the empty string) in the buffer. If *lineptr was NULL, FreeBSD allocates a 1-byte buffer. This is different from NetBSD and Illumos, because those systems don't put an empty string in the buffer.

In conclusion, when getline() reads EOF, the value of *lineptr might or might not change, and it might or might not point to an empty string.

Upvotes: 1

Stargateur
Stargateur

Reputation: 26757

If getline return an error (EOF is an error in this function). The data in the buffer should not be used.

EOF must not be returned if the function reads at least 1 byte, note that the function can return 0 in some case.

Additionally, the manual clearly say:

This buffer should be freed by the user program even if getline() failed.


In either case, on a successful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively.

This sentence could be interpreted as the buffer is only updated on a successful call.


In my opinion, the program should log this error and continue with the data that has been already read. Note: Use feof() to know if the stream has reached the end.

Upvotes: 5

Stuart
Stuart

Reputation: 1438

If you look at these POSIX docs http://pubs.opengroup.org/onlinepubs/9699919799/functions/getdelim.html you will see that the function does not return -1 if it reads the last line in the stream and the EOF occurs without a newline (i.e. the last line does not have a newline). So when you reach EOF, the contents of the buffer don't matter because getline will not have written anything into it.

Upvotes: 3

Related Questions