Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit c4fe235

Browse files
justinvpstephentoub
authored andcommitted
SslApplicationProtocol: Use the copied byte array (#25444)
When `copy` is true, the `protocol` byte array is copied to a new array, but then the new array is never used. Instead, use the new array.
1 parent be1e222 commit c4fe235

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/System.Net.Security/src/System/Net/Security/SslApplicationProtocol.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ internal SslApplicationProtocol(byte[] protocol, bool copy)
3333
if (copy)
3434
{
3535
byte[] temp = new byte[protocol.Length];
36-
Array.Copy(protocol, temp, protocol.Length);
37-
_readOnlyProtocol = new ReadOnlyMemory<byte>(protocol);
36+
Array.Copy(protocol, 0, temp, 0, protocol.Length);
37+
_readOnlyProtocol = new ReadOnlyMemory<byte>(temp);
3838
}
3939
else
4040
{

src/System.Net.Security/tests/UnitTests/SslApplicationProtocolTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ public void Constructor_Overloads_Succeeds()
3838
Assert.Throws<EncoderFallbackException>(() => { new SslApplicationProtocol("\uDC00"); });
3939
}
4040

41+
[Fact]
42+
public void Constructor_ByteArray_Copies()
43+
{
44+
byte[] expected = Encoding.UTF8.GetBytes("hello");
45+
SslApplicationProtocol byteProtocol = new SslApplicationProtocol(expected);
46+
47+
ArraySegment<byte> arraySegment;
48+
Assert.True(byteProtocol.Protocol.DangerousTryGetArray(out arraySegment));
49+
Assert.Equal(expected, arraySegment.Array);
50+
Assert.NotSame(expected, arraySegment.Array);
51+
}
52+
4153
[Theory]
4254
[MemberData(nameof(Protocol_Equality_TestData))]
4355
public void Equality_Tests_Succeeds(SslApplicationProtocol left, SslApplicationProtocol right)

0 commit comments

Comments
 (0)