Skip to content

Commit 76a9c29

Browse files
committed
fix: correctly init, read FileNameInfo buffer
Wrong buffer size. Wrong offsets. Redundant operations.
1 parent 500c7ec commit 76a9c29

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

deadlock-dotnet-sdk/Domain/SafeFileHandleEx.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,21 +412,16 @@ public unsafe (string? v, Exception? ex) FileNameInfo
412412
_ = GetFileInformationByHandleEx(this, FILE_INFO_BY_HANDLE_CLASS.FileNameInfo, &fni, (uint)Marshal.SizeOf(fni));
413413

414414
/** Get FileNameInfo */
415-
int bufferLength = (int)(fni.FileNameLength + Marshal.SizeOf(fni));
415+
int bufferLength = Marshal.SizeOf(fni.FileNameLength) + (int)fni.FileNameLength;
416416
using SafeBuffer<FILE_NAME_INFO> safeBuffer = new(numBytes: (nuint)bufferLength);
417417

418418
if (!GetFileInformationByHandleEx(this, FILE_INFO_BY_HANDLE_CLASS.FileNameInfo, (FILE_NAME_INFO*)safeBuffer.DangerousGetHandle(), (uint)bufferLength))
419419
return fileNameInfo = (null, new Exception(errFailedMsg + "GetFileInformationByHandleEx encountered an error.", new Win32Exception()));
420420

421-
UNICODE_STRING str = new()
422-
{
423-
Buffer = new PWSTR((char*)(((FILE_NAME_INFO*)safeBuffer.DangerousGetHandle()) + sizeof(int))),
424-
Length = (ushort)fni.FileNameLength,
425-
MaximumLength = (ushort)(bufferLength - sizeof(int))
426-
};
421+
string tmp = new((char*)(safeBuffer.DangerousGetHandle() + sizeof(uint)), 0, (int)fni.FileNameLength / 2);
427422

428423
/* The string conversion copies the data to a new string in the managed heap before freeing safeBuffer and leaving this context. */
429-
return fileNameInfo = ((string)str, null);
424+
return fileNameInfo = (tmp, null);
430425
}
431426
else
432427
{

0 commit comments

Comments
 (0)