Skip to content
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

Fix Windows AccessViolationException in FileSystemWatcher when monitoring network share for changes #42419

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ internal struct FILE_NOTIFY_INFORMATION
internal FileAction Action;

// Note that the file name is not null terminated
private uint FileNameLength;
private char _FileName;
internal uint FileNameLength;
internal char _FileName;
carlossanlop marked this conversation as resolved.
Show resolved Hide resolved

internal unsafe ReadOnlySpan<char> FileName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void ReadDirectoryChangesCallback(uint errorCode, uint numBytes, AsyncRe
}
else
{
ParseEventBufferAndNotifyForEach(state.Buffer);
ParseEventBufferAndNotifyForEach(state.Buffer, numBytes);
}
}
finally
Expand All @@ -245,20 +245,39 @@ private void ReadDirectoryChangesCallback(uint errorCode, uint numBytes, AsyncRe
}
}

private unsafe void ParseEventBufferAndNotifyForEach(byte[] buffer)
private unsafe void ParseEventBufferAndNotifyForEach(byte[] buffer, uint numBytes)
{
Debug.Assert(buffer != null);
Debug.Assert(buffer.Length > 0);
Debug.Assert(numBytes <= buffer.Length);
carlossanlop marked this conversation as resolved.
Show resolved Hide resolved

fixed (byte* b = buffer)
{
byte* pBufferLimit = b + numBytes;

Interop.Kernel32.FILE_NOTIFY_INFORMATION* info = (Interop.Kernel32.FILE_NOTIFY_INFORMATION*)b;
carlossanlop marked this conversation as resolved.
Show resolved Hide resolved

ReadOnlySpan<char> oldName = ReadOnlySpan<char>.Empty;

// Parse each event from the buffer and notify appropriate delegates
do
{
// Validate the data we received in case it's corrupted. This can happen
// if we are watching files over the network with a poor connection.
carlossanlop marked this conversation as resolved.
Show resolved Hide resolved

// Verify the info object is within the expected bounds
if (info < b || (((byte*)info) + sizeof(Interop.Kernel32.FILE_NOTIFY_INFORMATION)) > pBufferLimit)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (info < b || (((byte*)info) + sizeof(Interop.Kernel32.FILE_NOTIFY_INFORMATION)) > pBufferLimit)
if (info < b || ((byte*)info) + sizeof(Interop.Kernel32.FILE_NOTIFY_INFORMATION) > pBufferLimit)

jozkee marked this conversation as resolved.
Show resolved Hide resolved
{
break;
}

// Verify the file path is within the bounds
byte* pFileEnd = ((byte*)&(info->_FileName)) + info->FileNameLength;
if (pFileEnd < &(info->_FileName) || pFileEnd > pBufferLimit)
{
break;
}

// A slightly convoluted piece of code follows. Here's what's happening:
//
// We wish to collapse the poorly done rename notifications from the
Expand Down