]> git.ipfire.org Git - thirdparty/git.git/commitdiff
bisect: handle dup() failure when redirecting stdout
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Tue, 14 Jul 2026 22:48:44 +0000 (22:48 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Jul 2026 01:02:02 +0000 (18:02 -0700)
To capture the output of each verdict command, bisect_run()
temporarily redirects stdout to a temporary file via the classic
dup(1) / dup2() pair, restoring it afterwards. The return value of
dup(1) is not checked, however. When it fails, the saved descriptor
is -1, which is then passed to close() (the issue Coverity flags),
and the matching dup2() that is meant to restore stdout also fails,
leaving the process with stdout still pointing at the temporary file
for the remainder of the run.

Treat a failed dup(1) as a fatal error for this bisect step: close
the temporary file descriptor, report the error via error_errno(),
and break out of the loop so the existing cleanup path handles the
rest, just as on other failure paths in this function.

Reported by Coverity as CID 1508242 ("Improper use of negative
value").

Assisted-by: Opus 4.7
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/bisect.c

index 15a2a30f897b07f0b5d520158fad326624e5b712..801daf8c78560ce633c034c55d9565735d008c1e 100644 (file)
@@ -1308,6 +1308,11 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
 
                fflush(stdout);
                saved_stdout = dup(1);
+               if (saved_stdout < 0) {
+                       res = error_errno(_("could not duplicate stdout"));
+                       close(temporary_stdout_fd);
+                       break;
+               }
                dup2(temporary_stdout_fd, 1);
 
                res = bisect_state(terms, 1, &new_state);