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: 73151c54d5 ("syscalls.cc (stat_worker): Don't call build_fh_pc with invalid pc.")
Signed-off-by: Jon Turney <jon.turney@dronecode.org.uk>
This commit is contained in:
Jon Turney 2023-07-17 16:05:01 +01:00
parent 9fca983916
commit 42b44044b3
No known key found for this signature in database
GPG Key ID: C7C86F0370285C81
1 changed files with 4 additions and 3 deletions

View File

@ -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);