Merge branch 'tr/fd-gotcha-fixes'
authorJunio C Hamano <gitster@pobox.com>
Mon, 22 Jul 2013 18:23:12 +0000 (11:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Jul 2013 18:23:13 +0000 (11:23 -0700)
Two places we did not check return value (expected to be a file
descriptor) correctly.

* tr/fd-gotcha-fixes:
run-command: dup_devnull(): guard against syscalls failing
git_mkstemps: correctly test return value of open()

run-command.c
wrapper.c
index aece872e331caa28bf515c98f5cceb27d8414dff..1b7f88eeb1f1971f1568d5991978192c2d00645e 100644 (file)
@@ -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
index dd7ecbb115edd979f657e2e209126d364e6ccfac..6a015de5f0564e1ccc9c8cffca891f4ddf4a3ac3 100644 (file)
--- 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).