mailinfo & mailsplit: check for EOF while parsing

While POSIX states that it is okay to pass EOF to isspace() (and it seems to be implied that EOF should *not* be treated as whitespace), and also to pass EOF to ungetc() (which seems to be intended to fail without buffering the character), it is much better to handle these cases explicitly. Not only does it reduce head-scratching (and helps static analysis avoid reporting false positives), it also lets us handle files containing nothing but whitespace by erroring out. Reported via Coverity. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed May 4, 2017 at 15:56 UTC f0733c13ed8b79bb10e240c4b4a6630784c7d258
2 files changed +18 -1
builtin/mailsplit.c
+10
@@ -232,6 +232,16 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
232
233 do {
234 peek = fgetc(f);
235 + if (peek == EOF) {
236 + if (f == stdin)
237 + /* empty stdin is OK */
238 + ret = skip;
239 + else {
240 + fclose(f);
241 + error(_("empty mbox: '%s'"), file);
242 + }
243 + goto out;
244 + }
245 } while (isspace(peek));
246 ungetc(peek, f);
247
mailinfo.c
+8 -1
@@ -882,7 +882,10 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
882 for (;;) {
883 int peek;
884
885 - peek = fgetc(in); ungetc(peek, in);
885 + peek = fgetc(in);
886 + if (peek == EOF)
887 + break;
888 + ungetc(peek, in);
889 if (peek != ' ' && peek != '\t')
890 break;
891 if (strbuf_getline_lf(&continuation, in))
@@ -1099,6 +1102,10 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
1102
1103 do {
1104 peek = fgetc(mi->input);
1105 + if (peek == EOF) {
1106 + fclose(cmitmsg);
1107 + return error("empty patch: '%s'", patch);
1108 + }
1109 } while (isspace(peek));
1110 ungetc(peek, mi->input);
1111