From a2cb86c1524b7af20e609ca8d3db0b60b5c1d575 Mon Sep 17 00:00:00 2001 From: "Dale R. Worley" Date: Fri, 12 Jul 2013 10:58:35 +0200 Subject: [PATCH 1/2] git_mkstemps: correctly test return value of open() open() returns -1 on failure, and indeed 0 is a possible success value if the user closed stdin in our process. Fix the test. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- wrapper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wrapper.c b/wrapper.c index bac59d2c41..094b2ab983 100644 --- a/wrapper.c +++ b/wrapper.c @@ -322,7 +322,7 @@ int git_mkstemps_mode(char *pattern, int suffix_len, int mode) template[5] = letters[v % num_letters]; v /= num_letters; fd = open(pattern, O_CREAT | O_EXCL | O_RDWR, mode); - if (fd > 0) + if (fd >= 0) return fd; /* * Fatal error (EPERM, ENOSPC etc). From a77f106c7837faf6a712ea3ac720f5c4fa2feb07 Mon Sep 17 00:00:00 2001 From: Thomas Rast Date: Fri, 12 Jul 2013 10:58:36 +0200 Subject: [PATCH 2/2] run-command: dup_devnull(): guard against syscalls failing dup_devnull() did not check the return values of open() and dup2(). Fix this omission. Signed-off-by: Thomas Rast Signed-off-by: Junio C Hamano --- run-command.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/run-command.c b/run-command.c index 04712191e8..afc573ed41 100644 --- a/run-command.c +++ b/run-command.c @@ -76,7 +76,10 @@ static inline void close_pair(int fd[2]) static inline void dup_devnull(int to) { int fd = open("/dev/null", O_RDWR); - dup2(fd, to); + if (fd < 0) + die_errno(_("open /dev/null failed")); + if (dup2(fd, to) < 0) + die_errno(_("dup2(%d,%d) failed"), fd, to); close(fd); } #endif