Re: [PATCH v3 12/12] bisect: handle dup() failure when redirecting stdout
From: Jeff King <hidden>
Date: 2026-08-12 21:33:38
On Wed, Aug 12, 2026 at 08:03:20AM +0000, Johannes Schindelin via GitGitGadget wrote:
quoted hunk ↗ jump to hunk
@@ -1308,7 +1308,14 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv) fflush(stdout); saved_stdout = dup(1); - dup2(temporary_stdout_fd, 1); + if (saved_stdout < 0 || + dup2(temporary_stdout_fd, 1) < 0) { + res = error_errno(_("could not duplicate stdout")); + if (saved_stdout >= 0) + close(saved_stdout); + close(temporary_stdout_fd); + break; + }
OK. The extra "if (saved_stdout >= 0)" is not strictly necessary if we are OK considering close(-1) as a noop, but it doesn't hurt too much. It could also be avoided with two separate checks: saved_stdout = dup(1); if (saved_stdout < 0) ... if (dup2_temporary_stdout_fd, 1) < 0) ... but that would involve a little bit of repetition of the other cleanup lines (though it would also allow more specific error messages). Probably not worth polishing this further, though. What you have here is correct and I would be surprised if any user ever sees this error case. It is mostly about covering all of the paths for leaks. -Peff