Skip to content

Fix descriptor leaks in win32 fstat implementation #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 13, 2018

Conversation

cbookg
Copy link

@cbookg cbookg commented Jul 11, 2018

debug4("fstat - pio:%p", pio);
if (fd == -1) {
errno = EOTHER;
CloseHandle(dup_handle);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indentation. You also need to sign the contributor agreement.

@@ -769,14 +769,24 @@ fileio_write(struct w32_io* pio, const void *buf, size_t max_bytes)
int
fileio_fstat(struct w32_io* pio, struct _stat64 *buf)
{
int fd = _open_osfhandle((intptr_t)pio->handle, 0);
HANDLE dup_handle = 0;
if (!DuplicateHandle(GetCurrentProcess(), pio->handle, GetCurrentProcess(), &dup_handle, 0,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to duplicate handle here..
when you do _open_osfhandle it will return the c run-time file descriptor. If you close the c run-time file descriptor then it shouldn't have any impact on the os handle (pio->handle).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the docs for _open_osfhandle, _close will close the underlying handle:
"To close a file opened with , call _close. The underlying handle is also closed by a call to _close, so it is not necessary to call the Win32 function CloseHandle on the original handle"

Though I'm not an expert in these APIs.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I didn't read the msdn documentation properly.. You are right.

Copy link

@bagajjal bagajjal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

@cbookg
Copy link
Author

cbookg commented Jul 13, 2018

I've signed the CLA.

@manojampalam manojampalam merged commit ce3db0e into PowerShell:latestw_all Jul 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants