Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Adding null check for implicit cast from array to Span. (#25257)
Browse files Browse the repository at this point in the history
* Adding null check for implicit cast from array to Span.

* Addressing PR feedback - adding check for ArraySegment
  • Loading branch information
ahsonkhan authored Nov 18, 2017
1 parent 0102f69 commit 8a4934f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/System.Memory/src/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,13 @@ public override int GetHashCode()
/// <summary>
/// Defines an implicit conversion of an array to a <see cref="ReadOnlySpan{T}"/>
/// </summary>
public static implicit operator ReadOnlySpan<T>(T[] array) => new ReadOnlySpan<T>(array);
public static implicit operator ReadOnlySpan<T>(T[] array) => array != null ? new ReadOnlySpan<T>(array) : default;

/// <summary>
/// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="ReadOnlySpan{T}"/>
/// </summary>
public static implicit operator ReadOnlySpan<T>(ArraySegment<T> arraySegment) => new ReadOnlySpan<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
public static implicit operator ReadOnlySpan<T>(ArraySegment<T> arraySegment)
=> arraySegment.Array != null ? new ReadOnlySpan<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count) : default;

/// <summary>
/// Forms a slice out of the given read-only span, beginning at 'start'.
Expand Down
5 changes: 3 additions & 2 deletions src/System.Memory/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,13 @@ public override int GetHashCode()
/// <summary>
/// Defines an implicit conversion of an array to a <see cref="Span{T}"/>
/// </summary>
public static implicit operator Span<T>(T[] array) => new Span<T>(array);
public static implicit operator Span<T>(T[] array) => array != null ? new Span<T>(array) : default;

/// <summary>
/// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="Span{T}"/>
/// </summary>
public static implicit operator Span<T>(ArraySegment<T> arraySegment) => new Span<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
public static implicit operator Span<T>(ArraySegment<T> arraySegment)
=> arraySegment.Array != null ? new Span<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count) : default;

/// <summary>
/// Defines an implicit conversion of a <see cref="Span{T}"/> to a <see cref="ReadOnlySpan{T}"/>
Expand Down
27 changes: 27 additions & 0 deletions src/System.Memory/tests/ReadOnlySpan/ImplicitConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.SpanTests
{
public static partial class ReadOnlySpanTests
{
[Fact]
public static void NullImplicitCast()
{
int[] dst = null;
ReadOnlySpan<int> srcSpan = dst;
Assert.True(ReadOnlySpan<int>.Empty == srcSpan);
}

[Fact]
public static void ArraySegmentDefaultImplicitCast()
{
ArraySegment<int> dst = default;
ReadOnlySpan<int> srcSpan = dst;
Assert.True(ReadOnlySpan<int>.Empty == srcSpan);
}
}
}
27 changes: 27 additions & 0 deletions src/System.Memory/tests/Span/ImplicitConversion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;

namespace System.SpanTests
{
public static partial class SpanTests
{
[Fact]
public static void NullImplicitCast()
{
int[] dst = null;
Span<int> srcSpan = dst;
Assert.True(Span<int>.Empty == srcSpan);
}

[Fact]
public static void ArraySegmentDefaultImplicitCast()
{
ArraySegment<int> dst = default;
Span<int> srcSpan = dst;
Assert.True(Span<int>.Empty == srcSpan);
}
}
}
2 changes: 2 additions & 0 deletions src/System.Memory/tests/System.Memory.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<Compile Include="Span\GcReporting.cs" />
<Compile Include="Span\GetEnumerator.cs" />
<Compile Include="Span\GetHashCode.cs" />
<Compile Include="Span\ImplicitConversion.cs" />
<Compile Include="Span\IndexOf.T.cs" />
<Compile Include="Span\IndexOf.byte.cs" />
<Compile Include="Span\IndexOf.char.cs" />
Expand Down Expand Up @@ -58,6 +59,7 @@
<Compile Include="ReadOnlySpan\Equality.cs" />
<Compile Include="ReadOnlySpan\GetEnumerator.cs" />
<Compile Include="ReadOnlySpan\GetHashCode.cs" />
<Compile Include="ReadOnlySpan\ImplicitConversion.cs" />
<Compile Include="ReadOnlySpan\IndexOf.T.cs" />
<Compile Include="ReadOnlySpan\IndexOf.byte.cs" />
<Compile Include="ReadOnlySpan\IndexOf.char.cs" />
Expand Down

0 comments on commit 8a4934f

Please sign in to comment.