public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Cygwin: Fix Windows file handle leak in stat("file", -1)
@ 2023-07-21 12:26 Jon Turney
  0 siblings, 0 replies; only message in thread
From: Jon Turney @ 2023-07-21 12:26 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=42b44044b34d7c26abf84f8b24f820e04e1662f2

commit 42b44044b34d7c26abf84f8b24f820e04e1662f2
Author: Jon Turney <jon.turney@dronecode.org.uk>
Date:   Mon Jul 17 16:05:01 2023 +0100

    Cygwin: Fix Windows file handle leak in stat("file", -1)
    
    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 (which closes a Windows handle it has opened) if an
    exception happens in the __try block.
    
    Avoid this by re-ordering things so that we don't construct the fhandler
    object until after we've attempted to use the struct stat buffer.
    
    Fixes: 73151c54d581 ("syscalls.cc (stat_worker): Don't call build_fh_pc with invalid pc.")
    Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>

Diff:
---
 winsup/cygwin/syscalls.cc | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index 73343ecc1..32ace4d38 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -1967,12 +1967,13 @@ stat_worker (path_conv &pc, struct stat *buf)
 	{
 	  fhandler_base *fh;
 
-	  if (!(fh = build_fh_pc (pc)))
-	    __leave;
-
 	  debug_printf ("(%S, %p, %p), file_attributes %d",
 			pc.get_nt_native_path (), buf, fh, (DWORD) *fh);
 	  memset (buf, 0, sizeof (*buf));
+
+	  if (!(fh = build_fh_pc (pc)))
+	    __leave;
+
 	  res = fh->fstat (buf);
 	  if (!res)
 	    fh->stat_fixup (buf);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-21 12:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-21 12:26 [newlib-cygwin] Cygwin: Fix Windows file handle leak in stat("file", -1) Jon Turney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).