mailsplit: move NULL check before first use of file handle

The `split_mbox()` function calls fileno(f) to check whether the input is a terminal, but the NULL check for f (from `fopen()`) does not happen until later. When the file cannot be opened, f is NULL, and `fileno(NULL)` is undefined behavior, typically crashing with a segmentation fault. Move the NULL check above the `isatty()`/`fileno()` call so the error path is taken before any use of the potentially-NULL handle. Pointed out by Coverity. Assisted-by: Claude Opus 4.6 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 10, 2026 at 11:39 UTC 775f1f2b19e1c3d133356170373fedeb363988a3
1 file changed +3 -3
builtin/mailsplit.c
+3 -3
@@ -225,14 +225,14 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
225 FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
226 int file_done = 0;
227
228 - if (isatty(fileno(f)))
229 - warning(_("reading patches from stdin/tty..."));
230 -
228 if (!f) {
229 error_errno("cannot open mbox %s", file);
230 goto out;
231 }
232
233 + if (isatty(fileno(f)))
234 + warning(_("reading patches from stdin/tty..."));
235 +
236 do {
237 peek = fgetc(f);
238 if (peek == EOF) {