Merge pull request dotnet/corefx#39242 from scalablecory/38788-ping-race
authorGeoff Kizer <geoffrek@microsoft.com>
Fri, 12 Jul 2019 08:58:00 +0000 (01:58 -0700)
committerGitHub <noreply@github.com>
Fri, 12 Jul 2019 08:58:00 +0000 (01:58 -0700)
Fix a race in buffer usage of Http2Connection.ProcessPingFrame

Commit migrated from https://github.com/dotnet/corefx/commit/1d03a9106905278e82ee98ceee2d451b0e008000

1  2 
src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs

@@@ -815,13 -789,12 +820,12 @@@ namespace System.Net.Htt
              FrameHeader frameHeader = new FrameHeader(0, FrameType.Settings, FrameFlags.Ack, 0);
              frameHeader.WriteTo(writeBuffer);
  
 -            FinishWrite(mustFlush: true);
 +            FinishWrite(FlushTiming.AfterPendingWrites);
          }
  
-         private async Task SendPingAckAsync(ReadOnlyMemory<byte> pingContent)
+         /// <param name="pingContent">The 8-byte ping content to send, read as a big-endian integer.</param>
+         private async Task SendPingAckAsync(long pingContent)
          {
-             Debug.Assert(pingContent.Length == FrameHeader.PingLength);
              Memory<byte> writeBuffer = await StartWriteAsync(FrameHeader.Size + FrameHeader.PingLength).ConfigureAwait(false);
              if (NetEventSource.IsEnabled) Trace("Started writing.");
  
              frameHeader.WriteTo(writeBuffer);
              writeBuffer = writeBuffer.Slice(FrameHeader.Size);
  
-             pingContent.CopyTo(writeBuffer);
+             Debug.Assert(sizeof(long) == FrameHeader.PingLength);
+             BinaryPrimitives.WriteInt64BigEndian(writeBuffer.Span, pingContent);
  
 -            FinishWrite(mustFlush: false);
 +            FinishWrite(FlushTiming.AfterPendingWrites);
          }
  
          private async Task SendRstStreamAsync(int streamId, Http2ProtocolErrorCode errorCode)