Skip to content

Commit a11c396

Browse files
Thomas Rastgitster
authored andcommitted
git: ensure 0/1/2 are open in main()
Not having an open FD in the 0--2 range can lead to strange results, for example, a subsequent open() may return 2 (stderr) and then a die() would clobber this file. git-daemon and git-shell already guarded against this, but apparently users also manage to trip over it in other git commands. So we call sanitize_stdfds() during main git startup. Since these FDs are inherited, this covers all use of 'git foo ...', and all internal C commands when called directly. It does not fix shell/perl commands called directly. Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1d999dd commit a11c396

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

git.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@ int main(int argc, const char **argv)
505505
if (!cmd)
506506
cmd = "git-help";
507507

508+
/*
509+
* Always open file descriptors 0/1/2 to avoid clobbering files
510+
* in die(). It also avoids messing up when the pipes are dup'ed
511+
* onto stdin/stdout/stderr in the child processes we spawn.
512+
*/
513+
sanitize_stdfds();
514+
508515
git_setup_gettext();
509516

510517
/*

0 commit comments

Comments
 (0)