public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Jon Turney <jon.turney@dronecode.org.uk>
To: cygwin-patches@cygwin.com
Cc: Jon Turney <jon.turney@dronecode.org.uk>
Subject: [PATCH 2/2] Cygwin: Fix Windows file handle leak in stat("file", -1)
Date: Wed, 19 Jul 2023 13:41:42 +0100	[thread overview]
Message-ID: <20230719124142.10310-3-jon.turney@dronecode.org.uk> (raw)
In-Reply-To: <20230719124142.10310-1-jon.turney@dronecode.org.uk>

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.turney@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


  parent reply	other threads:[~2023-07-19 12:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-19 12:41 [PATCH 0/2] Testsuite adjustment and relevant fix Jon Turney
2023-07-19 12:41 ` [PATCH 1/2] Cygwin: testsuite: Drop setting TDIRECTORY Jon Turney
2023-07-19 12:41 ` Jon Turney [this message]
2023-07-19 15:33 ` [PATCH 0/2] Testsuite adjustment and relevant fix Corinna Vinschen
2023-07-20 12:55   ` Jon Turney
2023-07-20 15:14     ` Corinna Vinschen
2023-07-20 15:17       ` Corinna Vinschen
2023-07-21 12:26       ` Jon Turney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230719124142.10310-3-jon.turney@dronecode.org.uk \
    --to=jon.turney@dronecode.org.uk \
    --cc=cygwin-patches@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).