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

Why is MemorySize applied to WriteFile? #1555

Closed
AArnott opened this issue Apr 28, 2023 · 4 comments
Closed

Why is MemorySize applied to WriteFile? #1555

AArnott opened this issue Apr 28, 2023 · 4 comments

Comments

@AArnott
Copy link
Member

AArnott commented Apr 28, 2023

I expect arrays to be identified with the NativeArrayInfoAttribute, but WriteFile's lpBuffer parameter uses MemorySizeAttribute instead.

public unsafe static extern BOOL WriteFile([In] HANDLE hFile, [Optional][In][Const][MemorySize(BytesParamIndex = 2)] byte* lpBuffer, [In] uint nNumberOfBytesToWrite, [Optional][Out] uint* lpNumberOfBytesWritten, [Optional][In][Out] OVERLAPPED* lpOverlapped);

MemorySizeAttribute is already used elsewhere for a different purpose. Instead of describing the length of a byte array, it describes the size of just one struct as in SHGetFileInfo:

public unsafe static extern UIntPtr SHGetFileInfoW([In][Const] PWSTR pszPath, [In] FILE_FLAGS_AND_ATTRIBUTES dwFileAttributes, [Optional][In][Out][MemorySize(BytesParamIndex = 3)] SHFILEINFOW* psfi, [In] uint cbFileInfo, [In] SHGFI_FLAGS uFlags);

So it seems MemorySizeAttribute plays dual roles: it is used on pointers to arrays like lpBuffer and indicates the length of the array, and also on pointers to a single struct like psfi and indicates the size of one struct.

It is impossible to discern whether a pointer parameter with this attribute applied is an array or not without building some discrimination into it (e.g. byte* must mean length of an array, and all other parameter types must mean size of one struct).

Is this really by design? Or should we fix WriteFile to use NativeArrayInfo instead?

@AArnott
Copy link
Member Author

AArnott commented Apr 28, 2023

See also microsoft/CsWin32#913 where this caused a bug in CsWin32.

@mikebattista
Copy link
Collaborator

MemorySize is being applied instead of NativeArrayInfo when the SAL annotations use byteCount instead of elementCount.

@AArnott
Copy link
Member Author

AArnott commented Apr 28, 2023

Ok. I'll go forward with CsWin32's approach of special cases byte* then. Thanks.

@AArnott AArnott closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2023
@mikebattista
Copy link
Collaborator

I think the generic way of thinking of MemorySize is whatever you're passing in for this parameter, pass its size to the parameter indicated by BytesParamIndex.

@AArnott AArnott changed the title Why is MemorySize applied to CreateFile? Why is MemorySize applied to WriteFile? Sep 30, 2024
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

No branches or pull requests

2 participants