Web lists-archives.com

Re: [PATCH] strbuf: clear errno before calling getdelim(3)

Am 11.08.2017 um 09:50 schrieb Simon Ruderich:
> On Thu, Aug 10, 2017 at 10:56:40PM +0200, René Scharfe wrote:
>> getdelim(3) returns -1 at the end of the file and if it encounters an
>> error, but sets errno only in the latter case.  Set errno to zero before
>> calling it to avoid misdiagnosing an out-of-memory condition due to a
>> left-over value from some other function call.
> getdelim(3p) doesn't explicitly forbid changing the errno on EOF:
>      If no characters were read, and the end-of-file indicator for
>      the stream is set, or if the stream is at end-of-file, the
>      end-of-file indicator for the stream shall be set and the
>      function shall return −1. If an error occurs, the error
>      indicator for the stream shall be set, and the function shall
>      return −1 and set errno to indicate the error.
> So a valid implementation could still set errno on EOF and also
> on another error (where it's required to set errno).

True, especially the part that other errors are possible.  But we can't
rely on errno being set on EOF because leaving it unchanged is allowed
as well in that

> I don't think that it matters in practice, but the "most" correct
> way to handle this would be to check if feof(3) is true to check
> for the non-errno case.

Only if errors at EOF are guaranteed to be impossible.  Imagine
getdelim being able to read to the end and then failing to get memory
for the final NUL.  Other scenarios may be possible.