Don't leak a Windows file handle if stat() is called with a valid
filename, but invalid stat buffer pointer.

We do not destroy fh if an exception happens in the __try block, which
closes a Windows handle it has opened.

Fixes: 73151c54d581 ("syscalls.cc (stat_worker): Don't call build_fh_pc with 
invalid pc.")
Signed-off-by: Jon Turney <jon.tur...@dronecode.org.uk>
---
 winsup/cygwin/syscalls.cc | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 73343ecc1..c6999407e 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -1955,6 +1955,7 @@ int
 stat_worker (path_conv &pc, struct stat *buf)
 {
   int res = -1;
+  fhandler_base *fh = NULL;
 
   __try
     {
@@ -1965,8 +1966,6 @@ stat_worker (path_conv &pc, struct stat *buf)
        }
       else if (pc.exists ())
        {
-         fhandler_base *fh;
-
          if (!(fh = build_fh_pc (pc)))
            __leave;
 
@@ -1976,13 +1975,14 @@ stat_worker (path_conv &pc, struct stat *buf)
          res = fh->fstat (buf);
          if (!res)
            fh->stat_fixup (buf);
-         delete fh;
        }
       else
        set_errno (ENOENT);
     }
   __except (EFAULT) {}
   __endtry
+
+  delete fh;
   syscall_printf ("%d = (%S,%p)", res, pc.get_nt_native_path (), buf);
   return res;
 }
-- 
2.39.0

Reply via email to