From: Jeff King Date: Thu, 27 Apr 2017 06:21:14 GMT Subject: Re: [PATCH 10/26] Check for EOF while parsing mails Message-ID: <20170427062114.p367j6rojnw4aj5r@sigill.intra.peff.net> In-Reply-To: <1fb841cee32996ee9194c2bd33b9dfe74cc37726.1493237937.git.johannes.schindelin@gmx.de> On Wed, Apr 26, 2017 at 10:20:16PM +0200, Johannes Schindelin wrote: > diff --git a/builtin/mailsplit.c b/builtin/mailsplit.c > index 30681681c13..c0d88f97512 100644 > --- a/builtin/mailsplit.c > +++ b/builtin/mailsplit.c > @@ -232,7 +232,7 @@ static int split_mbox(const char *file, const char *dir, int allow_bare, > > do { > peek = fgetc(f); > - } while (isspace(peek)); > + } while (peek >= 0 && isspace(peek)); > ungetc(peek, f); Are we guaranteed that EOF is a negative number? Also, what is the behavior of ungetc when we pass it EOF? It looks like POSIX does what we want (pushing EOF is a noop, and the stream retains its feof() status), but I don't know if there are other implementations to worry about. Perhaps: /* soak up whitespace */ while ((peek = fgetc(f)) != EOF) { if (!isspace(peek)) { ungetc(peek, f); break; } } would be more portable. -Peff