Browse Source

fix bug in RefreshBuffer

Jan Tattermusch 5 years ago
parent
commit
3cdc107bda
1 changed files with 3 additions and 0 deletions
  1. 3 0
      csharp/src/Google.Protobuf/WriteBufferHelper.cs

+ 3 - 0
csharp/src/Google.Protobuf/WriteBufferHelper.cs

@@ -80,6 +80,7 @@ namespace Google.Protobuf
             {
             {
                 // because we're using coded output stream, we know that "buffer" and codedOutputStream.InternalBuffer are identical.
                 // because we're using coded output stream, we know that "buffer" and codedOutputStream.InternalBuffer are identical.
                 codedOutputStream.InternalOutputStream.Write(codedOutputStream.InternalBuffer, 0, state.position);
                 codedOutputStream.InternalOutputStream.Write(codedOutputStream.InternalBuffer, 0, state.position);
+                // reset position, limit stays the same because we are reusing the codedOutputStream's internal buffer.
                 state.position = 0;
                 state.position = 0;
             }
             }
             else if (bufferWriter != null)
             else if (bufferWriter != null)
@@ -88,6 +89,7 @@ namespace Google.Protobuf
                 bufferWriter.Advance(state.position);
                 bufferWriter.Advance(state.position);
                 state.position = 0;
                 state.position = 0;
                 buffer = bufferWriter.GetSpan();
                 buffer = bufferWriter.GetSpan();
+                state.limit = buffer.Length;
             }
             }
             else
             else
             {
             {
@@ -108,6 +110,7 @@ namespace Google.Protobuf
             {
             {
                 bufferWriter.Advance(state.position);
                 bufferWriter.Advance(state.position);
                 state.position = 0;
                 state.position = 0;
+                state.limit = 0;
                 buffer = default;  // invalidate the current buffer
                 buffer = default;  // invalidate the current buffer
                 // TODO: add a test when we flush and then try to write more data
                 // TODO: add a test when we flush and then try to write more data
             }
             }