On Tue, Jan 17, 2017 at 07:15:49PM -0600, Derek Martin wrote:
> On Sun, Jan 15, 2017 at 11:33:41AM +0100, Simon Ruderich wrote:
> [Re-quoting the original patch to provide proper context]
> +    if (!feof(f) && ch)
> +      ungetc(ch, f);
> 
> > ch will be either EOF on error or end-of-file or contain the
> > first byte. Why the check for != 0 here? This will break if there
> > was an error during reading and will put EOF (Oxff) into the
> > stream. 
> 
> It will do no such thing.  

Sorry, I realized I slightly misread the comment.  On ERROR, it may
well put EOF into the file stream (though I'm not sure--the file is in
an error state so not sure what the actual behavior is here), though
again that's hardly a concern.   If the file can't be read due to an
error, there's no practical difference in the behavior.  In both cases
the subsequent fgets() will fail due to the error, and return... you
guessed it, EOF.  In either case, the subsequent strncmp() will be
compared to EOF, and the function will fail, return -1, and log that
it can't read the file.

It might be slightly preferable to change the if block to:

  if (!feof(f) && !ferror(f) && ch)
    ungetc(ch, f);

But it makes exactly zero difference.  Also:

  if (!feof(f) && !ferror(f))
    ungetc(ch, f);

Not clearly better.


-- 
Derek D. Martin    http://www.pizzashack.org/   GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address.  Replying to it will result in
undeliverable mail due to spam prevention.  Sorry for the inconvenience.

Attachment: pgpNUjv_nU51M.pgp
Description: PGP signature

Reply via email to