]> git.ipfire.org Git - thirdparty/git.git/commit
run-command: avoid `close(-1)` in `start_command()` error paths
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Sun, 5 Jul 2026 08:24:21 +0000 (08:24 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 5 Jul 2026 16:12:09 +0000 (09:12 -0700)
commitc3f89beb733a260866ec9c163615bce59e77481b
tree69018ccd0f90f4ab2f1883a0647adacce20eba0f
parenta62c2a26fcedb635046f8d072fc9d9629e6e87ba
run-command: avoid `close(-1)` in `start_command()` error paths

When `start_command()` fails to set up a pipe partway through, it rolls
back by closing the pipe ends it has already opened. For descriptors
supplied by the caller rather than allocated locally, that rollback
tested `if (cmd->in)` / `if (cmd->out)` before calling close(). The
CHILD_PROCESS_INIT default of -1 ("no descriptor") is non-zero and so
passes the test, meaning a caller that sets cmd->no_stdin or
cmd->no_stdout without supplying a real fd ends up triggering close(-1)
on the error path.

The stdin-pipe failure branch a few lines above already uses the right
idiom, `if (cmd->out > 0)`, which rejects both the -1 sentinel and 0
(the parent's own standard streams). Apply it to the three remaining
rollback sites.

Reported by Coverity as CID 1049722 ("Argument cannot be negative").

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