Skip to content

Commit f0733c1

Browse files
dschogitster
authored andcommitted
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>
1 parent e7b65e2 commit f0733c1

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

builtin/mailsplit.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,16 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
232232

233233
do {
234234
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+
}
235245
} while (isspace(peek));
236246
ungetc(peek, f);
237247

mailinfo.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,10 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
882882
for (;;) {
883883
int peek;
884884

885-
peek = fgetc(in); ungetc(peek, in);
885+
peek = fgetc(in);
886+
if (peek == EOF)
887+
break;
888+
ungetc(peek, in);
886889
if (peek != ' ' && peek != '\t')
887890
break;
888891
if (strbuf_getline_lf(&continuation, in))
@@ -1099,6 +1102,10 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
10991102

11001103
do {
11011104
peek = fgetc(mi->input);
1105+
if (peek == EOF) {
1106+
fclose(cmitmsg);
1107+
return error("empty patch: '%s'", patch);
1108+
}
11021109
} while (isspace(peek));
11031110
ungetc(peek, mi->input);
11041111

0 commit comments

Comments
 (0)