]> git.ipfire.org Git - thirdparty/git.git/commitdiff
mailsplit: move NULL check before first use of file handle
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 10 Jul 2026 11:39:29 +0000 (11:39 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Jul 2026 15:13:54 +0000 (08:13 -0700)
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>
builtin/mailsplit.c

index 264df6259a04118f78a3adee6f2574e2332c2502..0993418e630dec3f8085421c2fbf9ac3995deef1 100644 (file)
@@ -225,14 +225,14 @@ static int split_mbox(const char *file, const char *dir, int allow_bare,
        FILE *f = !strcmp(file, "-") ? stdin : fopen(file, "r");
        int file_done = 0;
 
-       if (isatty(fileno(f)))
-               warning(_("reading patches from stdin/tty..."));
-
        if (!f) {
                error_errno("cannot open mbox %s", file);
                goto out;
        }
 
+       if (isatty(fileno(f)))
+               warning(_("reading patches from stdin/tty..."));
+
        do {
                peek = fgetc(f);
                if (peek == EOF) {