]> git.ipfire.org Git - thirdparty/git.git/blobdiff - compat/winansi.c
mingw: work around incorrect standard handles
[thirdparty/git.git] / compat / winansi.c
index 54fd701cbfb44907b93e6b331bfb91ca80a87a94..c27b20a79d91cf7877d795c96e64cbadafdad7fd 100644 (file)
@@ -662,10 +662,20 @@ void winansi_init(void)
  */
 HANDLE winansi_get_osfhandle(int fd)
 {
+       HANDLE ret;
+
        if (fd == 1 && (fd_is_interactive[1] & FD_SWAPPED))
                return hconsole1;
        if (fd == 2 && (fd_is_interactive[2] & FD_SWAPPED))
                return hconsole2;
 
-       return (HANDLE)_get_osfhandle(fd);
+       ret = (HANDLE)_get_osfhandle(fd);
+
+       /*
+        * There are obviously circumstances under which _get_osfhandle()
+        * returns (HANDLE)-2. This is not documented anywhere, but that is so
+        * clearly an invalid handle value that we can just work around this
+        * and return the correct value for invalid handles.
+        */
+       return ret == (HANDLE)-2 ? INVALID_HANDLE_VALUE : ret;
 }