Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Ensure the async reader state resets the BOM offset in every AdvanceB…
…uffer() call.
  • Loading branch information
eiriktsarpalis authored and github-actions committed Nov 11, 2022
commit b84c3df846ec00b0a5a965b7da8dbd3ec93ccd7e
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public void AdvanceBuffer(int bytesConsumed)
// Copy the unprocessed data to the new buffer while shifting the processed bytes.
Buffer.BlockCopy(oldBuffer, _offset + bytesConsumed, newBuffer, 0, _count);
_buffer = newBuffer;
_offset = 0;
_maxCount = _count;

// Clear and return the old buffer
Expand All @@ -133,9 +132,10 @@ public void AdvanceBuffer(int bytesConsumed)
{
// Shift the processed bytes to the beginning of buffer to make more room.
Buffer.BlockCopy(_buffer, _offset + bytesConsumed, _buffer, 0, _count);
_offset = 0;
}
}

_offset = 0;
}

private void ProcessReadBytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,31 @@ public static void InvalidJsonShouldFailAtAnyPosition_Sequence(
Assert.Equal(expectedFailure.Column, ex.BytePositionInLine);
}

[Fact]
public static async Task BomHandlingRegressionTest()
{
byte[] utf8Bom = Encoding.UTF8.GetPreamble();
byte[] json = """{ "Value" : "Hello" }"""u8.ToArray();

using var stream = new MemoryStream();
stream.Write(utf8Bom, 0, utf8Bom.Length);
stream.Write(json, 0, json.Length);
stream.Position = 0;

var options = new JsonSerializerOptions
{
DefaultBufferSize = 32
};

Test result = await JsonSerializer.DeserializeAsync<Test>(stream, options);
Assert.Equal("Hello", result.Value);
}

private class Test
{
public string Value { get; set; }
}

private class Chunk : ReadOnlySequenceSegment<byte>
{
public Chunk(string json, int firstSegmentLength)
Expand Down