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

Fixing 0 length issue with constructor #107427

Merged

Conversation

michaelgsharp
Copy link
Member

Fixes #106538 by modifying the check in the constructor to also throw an error if the base memory length is 0.

@rameel
Copy link

rameel commented Sep 6, 2024

This expression can be simplified...

if ((maxElements >= span.Length && span.Length != 0) || (span.Length == 0 && maxElements > 0))
    ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

... to:

if (maxElements > 0 && maxElements >= span.Length)
    ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

Explanation:

For simplicity, let's represent maxElements as a and span.Length as b. Now, the condition looks like this:

if ((a >= b && b != 0) || (b == 0 && a > 0))
    error;

We can normalize the condition by switching the order of the expressions (b == 0 && a > 0), resulting in:

if ((a >= b && b != 0) || (a > 0 && b == 0))
    error;

This can be interpreted as:

if ((a >= b && b != 0)  // a > 0, b > 0, a >= b
 || (a > 0  && b == 0)) // a > 0, b = 0
    error;

Thus, we are only interested in cases where a > 0, which simplifies the second part of the expression.

In the end, we get:

if (a > 0 && a >= b)
    error;

The final expression will look like this:

if (maxElements > 0 && maxElements >= span.Length)
    ThrowHelper.ThrowArgument_InvalidStridesAndLengths();

…ors/netcore/ReadOnlyTensorSpan.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@michaelgsharp
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

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

@michaelgsharp michaelgsharp merged commit 9eef776 into dotnet:main Sep 16, 2024
81 of 85 checks passed
@michaelgsharp michaelgsharp deleted the tensor-span-monotonic-constructor branch September 16, 2024 19:18
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 17, 2024
* fixing 0 length issue with constructor

* Update src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* comments from PR

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
sirntar pushed a commit to sirntar/runtime that referenced this pull request Sep 30, 2024
* fixing 0 length issue with constructor

* Update src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/ReadOnlyTensorSpan.cs

Co-authored-by: Stephen Toub <stoub@microsoft.com>

* comments from PR

---------

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators Oct 17, 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 Non-monotonic constructor errors
4 participants