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

WindowsRuntimeBufferExtensions.ToArray incorrectly checks Capacity #23383

Closed
jefffhaynes opened this issue Aug 30, 2017 · 14 comments
Closed

WindowsRuntimeBufferExtensions.ToArray incorrectly checks Capacity #23383

jefffhaynes opened this issue Aug 30, 2017 · 14 comments
Labels
area-System.Runtime.InteropServices help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@jefffhaynes
Copy link

The following code will throw an ArgumentException:

var buffer = new Windows.Storage.Streams.Buffer(0);
var array = buffer.ToArray();

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.

@danmoseley
Copy link
Member

@jefffhaynes I think you're right. Would you like to submit a PR maybe?

@jefffhaynes
Copy link
Author

jefffhaynes commented Aug 30, 2017 via email

@jefffhaynes
Copy link
Author

So far no luck getting the repo to build and test cleanly. I'll work on it more this weekend.

@jefffhaynes
Copy link
Author

jefffhaynes commented Sep 2, 2017

Maybe you can help me a little - the following does not seem to actually do anything:

build src\System.Runtime.WindowsRuntime\src

Similarly build src\System.Runtime.WindowsRuntime\tests does not seem to do anything. In contrast, running src\System.Collections\src does appear to do something. However, if I make a breaking change somewhere in Systems.Collection and build again it still succeeds. I have not been able to ascertain how to clean a specific directory. I also have been unable to get Visual Studio to build anything despite following the directions on here.

But more perplexing is if I make a breaking change to System.Runtime.WindowsRuntime, clean, and build from root, the build succeeds. This would seem to suggest that somehow that area isn't being built at all.

I'm sure I'm missing something here but I'm kind of stuck. Happy to help if you can point me in the right direction.

EDIT: I should also note that System.Runtime.InteropServices also seems to build but does not seem to build (for example) WindowsRuntimeBufferExtensions.cs.

@juliushardt
Copy link
Contributor

As I see it, WindowsRuntimeBufferExtensions.ToArray() is working as expected.
If you call ToArray() on a buffer of length zero, you effectively get WindowsRuntimeBufferExtensions.ToArray(buffer, sourceIndex: 0, count: buffer.Length) because the former calls the latter.

Since sourceIndex and the capacity of the source are both 0, an exception is thrown. According to the API docs, this behaviour is intended (emphasis mine):

//   T:System.ArgumentException:
//     sourceIndex is greater than or equal to the capacity of source. -or-The number
//     of bytes in source, beginning at sourceIndex, is less than count.
[CLSCompliant(false)]
public static byte[] ToArray(this IBuffer source, uint sourceIndex, int count);

Here's the corresponding unit test.

Missing docs

It looks like the API docs for this method are currently not available on docs.microsoft.com. I created an issue on the docs repo.

Additionally, I think that the docs for the short overload should clarify that calling it on an empty buffer throws. We should file a separate issue once the docs are up again.

Building System.Runtime.WindowsRuntime

I stumbled across that, too, @jefffhaynes. In order to build System.Runtime.WindowsRuntime, you need to add a parameter to the build command: build.cmd -framework=uap, as it defaults to "netcoreapp" if it is omitted. You can read more about that here.

@jefffhaynes
Copy link
Author

jefffhaynes commented Sep 26, 2018 via email

@GSPP
Copy link

GSPP commented Dec 11, 2018

sourceIndex is greater than or equal to

This should be changed. This should not break any practical code. It's useful, and common, to allow taking a zero length slice from the end of a sequence.

@jefffhaynes
Copy link
Author

Just to rant momentarily, something like this never would have been a discussion with .net “classic” and it would have been fixed by now. It’s bugs like this that are still preventing UWP from being more widely adopted.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@jeffschwMSFT jeffschwMSFT removed the untriaged New issue has not been triaged by the area owner label Feb 24, 2020
@iSazonov
Copy link
Contributor

@jeffschwMSFT Is WindowsRuntimeBufferExtensions removed from the repository?

@jeffschwMSFT
Copy link
Member

In .NET 5 Preview 6 we removed the built in WinRT support. You can see our blog for details (https://devblogs.microsoft.com/dotnet/announcing-net-5-0-preview-6/). The WinRT support is now being handled by https://github.com/microsoft/CsWinRT.

cc @AaronRobinsonMSFT @jkoritzinsky

@iSazonov
Copy link
Contributor

@jeffschwMSFT Thanks! I wonder that the issue is still open here.

@jkoritzinsky
Copy link
Member

I think we just missed this issue. I think we should move this issue to microsoft/CsWinRT, but I don't have the right tools installed to do that transfer. @jeffschwMSFT can you port this issue to microsoft/CsWinRT?

@jeffschwMSFT
Copy link
Member

@jkoritzinsky I think the best option would be to copy this content into a new issue and close this one.

@AaronRobinsonMSFT
Copy link
Member

Moved to CsWinRT: microsoft/CsWinRT#334

@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

10 participants