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?
If getline return an error (EOF is an error in this function). The data in the buffer should not be used.
EOFmust 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 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.