Skip to content

Commit a68bf7b

Browse files
michaelgsharpstephentoub
authored andcommitted
Fixing 0 length issue with constructor (dotnet#107427)
* 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>
1 parent f6e0766 commit a68bf7b

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public ReadOnlyTensorSpan(ReadOnlySpan<T> span, scoped ReadOnlySpan<nint> length
129129
strides = strides.IsEmpty ? (ReadOnlySpan<nint>)TensorSpanHelpers.CalculateStrides(lengths, linearLength) : strides;
130130
TensorSpanHelpers.ValidateStrides(strides, lengths);
131131
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
132-
if (maxElements >= span.Length && span.Length != 0)
132+
if (span.IsEmpty ? maxElements != 0 : maxElements >= span.Length)
133133
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();
134134

135135
_shape = new TensorShape(span.Length, lengths, strides);

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpan.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public TensorSpan(Span<T> span, scoped ReadOnlySpan<nint> lengths, scoped ReadOn
132132
TensorSpanHelpers.ValidateStrides(strides, lengths);
133133

134134
nint maxElements = TensorSpanHelpers.ComputeMaxLinearIndex(strides, lengths);
135-
if (maxElements >= span.Length && span.Length != 0)
135+
if (span.IsEmpty ? maxElements != 0 : maxElements >= span.Length)
136136
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();
137137

138138
_shape = new TensorShape(span.Length, lengths, strides);

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorSpanHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ public static nint ComputeLinearIndex(ReadOnlySpan<NIndex> indexes, ReadOnlySpan
190190

191191
public static void ValidateStrides(ReadOnlySpan<nint> strides, ReadOnlySpan<nint> lengths)
192192
{
193-
Debug.Assert(strides.Length == lengths.Length);
193+
if (strides.Length != lengths.Length)
194+
ThrowHelper.ThrowArgument_InvalidStridesAndLengths();
194195

195196
if (strides.Length == 0)
196197
return;

src/libraries/System.Numerics.Tensors/tests/ReadOnlyTensorSpanTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ public static void ReadOnlyTensorSpanSystemArrayConstructorTests()
230230
[Fact]
231231
public static void ReadOnlyTensorSpanArrayConstructorTests()
232232
{
233+
// Make sure exception is thrown if lengths and strides would let you go past the end of the array
234+
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[0], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
235+
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[1], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
236+
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[2], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 2 }));
237+
233238
// Make sure basic T[] constructor works
234239
int[] a = { 91, 92, -93, 94 };
235240
scoped ReadOnlyTensorSpan<int> spanInt = new ReadOnlyTensorSpan<int>(a);

src/libraries/System.Numerics.Tensors/tests/TensorSpanTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,11 @@ public static void TensorSpanSystemArrayConstructorTests()
772772
[Fact]
773773
public static void TensorSpanArrayConstructorTests()
774774
{
775+
// Make sure exception is thrown if lengths and strides would let you go past the end of the array
776+
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[0], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
777+
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[1], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 1 }));
778+
Assert.Throws<ArgumentException>(() => new TensorSpan<double>(new double[2], lengths: new IntPtr[] { 2 }, strides: new IntPtr[] { 2 }));
779+
775780
// Make sure basic T[] constructor works
776781
int[] a = { 91, 92, -93, 94 };
777782
scoped TensorSpan<int> spanInt = new TensorSpan<int>(a);

0 commit comments

Comments
 (0)