line-log: fix crash when --first-parent is used
[gitweb.git] / setup.c
diff --git a/setup.c b/setup.c
index 0d9ea6239f2512760b4104440362d2e3758b3f09..5262319be69b3bf3fd2492dea3d30e7353d66a23 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -911,3 +911,15 @@ const char *resolve_gitdir(const char *suspect)
                return suspect;
        return read_gitfile(suspect);
 }
+
+/* if any standard file descriptor is missing open it to /dev/null */
+void sanitize_stdfds(void)
+{
+       int fd = open("/dev/null", O_RDWR, 0);
+       while (fd != -1 && fd < 2)
+               fd = dup(fd);
+       if (fd == -1)
+               die_errno("open /dev/null or dup failed");
+       if (fd > 2)
+               close(fd);
+}