dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

System.Numerics.Tensors Non-monotonic constructor errors #106538

Open soerenwolfers opened 4 weeks ago

soerenwolfers commented 4 weeks ago

Description

new TensorSpan<double>(new double[j], lengths: new IntPtr[] {2}, strides: new IntPtr[] {1}); 

throws a ArgumentException if j == 1 but not if j == 0 or j > 1.

Reproduction Steps

using System.Diagnostics.CodeAnalysis;
using System.Numerics.Tensors;

namespace ConsoleApp1;

public abstract class Program
{
    [Experimental("SYSLIB5001")]
    public static void Main()
    {
        new TensorSpan<double>(new double[0], lengths: new IntPtr[] {2}, strides: new IntPtr[] {1});
        new TensorSpan<double>(new double[2], lengths: new IntPtr[] {2}, strides: new IntPtr[] {1});
        new TensorSpan<double>(new double[1], lengths: new IntPtr[] {2}, strides: new IntPtr[] {1}); 
    }
}

Expected behavior

Error when j <= 1

Actual behavior

Error if and only if j == 1

Regression?

No response

Known Workarounds

No response

Configuration

.NET8, Ubuntu22, x64

Other information

No response

dotnet-policy-service[bot] commented 4 weeks ago

Tagging subscribers to this area: @dotnet/area-system-numerics See info in area-owners.md if you want to be subscribed.

dotnet-policy-service[bot] commented 4 weeks ago

Tagging subscribers to this area: @dotnet/area-system-numerics-tensors See info in area-owners.md if you want to be subscribed.

michaelgsharp commented 1 week ago

Ok, this should throw when j = 0 || j = 1, but not when j > 1. I'll fix the 0 case. As long as there is enough backing memory its ok if there is more backing memory than the lengths/span can actually access.