Skip to content

Conversation

@michaelgsharp
Copy link
Contributor

Fixes #106534. Fixes the check in the System.Array constructor to make sure the type of the Array matches the T that was used.

return; // returns default
}
if (!typeof(T).IsValueType && array.GetType() != typeof(T[]))
if (array.GetType().GetElementType() != typeof(T))
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a correct fix. But I'd like us to get an issue logged around this, as this seems like a pit of failure here.

In particular, since Array is "untyped" and due to how new[] {...} works, it means that users can get into these types of silent failures; when they likely expected it to be implicitly typed as double[] like [...] would've been.

So I think we may want to consider if this should be a constructor or instead some named Create* method where * is an appropriate suffix.

@michaelgsharp
Copy link
Contributor Author

/backport to release/9.0

@github-actions
Copy link
Contributor

github-actions bot commented Sep 9, 2024

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/10779752164

@michaelgsharp
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

You have several pipelines (over 10) configured to build pull requests in this repository. Specify which pipelines you would like to run by using /azp run [pipelines] command. You can specify multiple pipelines using a comma separated list.

@michaelgsharp michaelgsharp force-pushed the array-constructor-check branch from a706e72 to bee3313 Compare September 13, 2024 20:40
@michaelgsharp michaelgsharp merged commit 16506b7 into dotnet:main Sep 16, 2024
@michaelgsharp michaelgsharp deleted the array-constructor-check branch September 16, 2024 04:01
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 17, 2024
* fixing System.Array constructor

* adding back in co-varience check and tests
sirntar pushed a commit to sirntar/runtime that referenced this pull request Sep 30, 2024
* fixing System.Array constructor

* adding back in co-varience check and tests
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System.Numerics.Tensors constructor safety

3 participants