-
Notifications
You must be signed in to change notification settings - Fork 107
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
WindowsRuntimeBufferExtensions.ToArray incorrectly checks Capacity #334
Comments
For what its worth this edge case is also present in other methods of |
This comment suggests making the condition in ToArray a strict greater-than. |
The other methods this check happens in is CopyTo. I tried the following code, with no change to the exception condition:
And there was no error, so I think changing the condition there is unnecessary. |
The fix was incomplete, there are various extension methods in WindowsRuntimeBufferExtensions which still throw incorrectly. |
@weltkante Can you be more descriptive and/or give examples? Of the remaining public methods: AsBuffer, CopyTo and GetByte, I do not see a situation like you describe of count being zero causing an index check against capacity to incorrectly throw. I see in CopyTo (for byte[] -> Buffer) there is a check of source.Length - sourceIndex < count ; but I do not see how this can cause false errors |
The cause of the false checks are incorrect An index can be at the end of the buffer when count is zero, see my comment above, so many of the extension methods incorrectly throw in this case even though they do not throw if length is zero at any place except the end of buffer. If that description isn't enough and you need a full list of failure scenarios I can do one tomorrow, its a bit late today on my end. |
@weltkante thanks for your reply, do you think changing to |
I think its an off-by-one error and changing Generally the invariants for upper bounds are that |
@weltkante I've made some additional changes, review/feedback would be greatly appreciated! #504 |
Issue moved from dotnet/runtime - see dotnet/runtime#23383
The following code will throw an ArgumentException:
WindowsRuntimeBufferExtensions.ToArray checks the buffer capacity as a precondition, which isn't used in the method. Obviously the expected behavior would be to return an array of length zero.
I also can't find a unit test for this case.
The text was updated successfully, but these errors were encountered: