Skip to content

[release/dev17.14] Fix aligned buffer write #11862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.Language.Intermediate;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.AspNetCore.Razor.Language.CodeGeneration;
Expand Down Expand Up @@ -439,4 +440,46 @@ class C

""", output);
}

[Fact, WorkItem("https://github.com/dotnet/core/issues/9885")]
public void AlignedPages_WritesCorrectlyWhenPageAndBufferAreAligned()
{
var pages = new LinkedList<ReadOnlyMemory<char>[]>();

const string FirstLine = "First Line";
pages.AddLast([(FirstLine + FirstLine).AsMemory(), "Second".AsMemory()]);

var testReader = CodeWriter.GetTestTextReader(pages);
var output = new char[FirstLine.Length];

testReader.Read(output, 0, output.Length);
Assert.Equal(FirstLine, string.Join("", output));
Array.Clear(output, 0, output.Length);

testReader.Read(output, 0, output.Length);
Assert.Equal(FirstLine, string.Join("", output));
Array.Clear(output, 0, output.Length);

testReader.Read(output, 0, output.Length);
Assert.Equal("Second\0\0\0\0", string.Join("", output));
}

[Fact]
public void ReaderOnlyReadsAsMuchAsRequested()
{
var pages = new LinkedList<ReadOnlyMemory<char>[]>();

const string FirstLine = "First Line";
pages.AddLast([FirstLine.AsMemory()]);

var testReader = CodeWriter.GetTestTextReader(pages);
var output = new char[FirstLine.Length];

testReader.Read(output, 0, 2);
Assert.Equal("Fi\0\0\0\0\0\0\0\0", string.Join("", output));
Array.Clear(output, 0, output.Length);

testReader.Read(output, 0, output.Length);
Assert.Equal("rst Line\0\0", string.Join("", output));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,20 @@ public CodeWriter WriteLine([InterpolatedStringHandlerArgument("")] ref WriteInt

public SourceText GetText()
{
using var reader = new Reader(this);
using var reader = new Reader(_pages, Length);
return SourceText.From(reader, Length, Encoding.UTF8);
}

private sealed class Reader(CodeWriter codeWriter) : TextReader
// Internal for testing
internal static TextReader GetTestTextReader(LinkedList<ReadOnlyMemory<char>[]> pages)
{
private LinkedListNode<ReadOnlyMemory<char>[]>? _page = codeWriter._pages.First;
private int _remainingLength = codeWriter.Length;
return new Reader(pages, pages.Count);
}

private sealed class Reader(LinkedList<ReadOnlyMemory<char>[]> pages, int length) : TextReader
{
private LinkedListNode<ReadOnlyMemory<char>[]>? _page = pages.First;
private int _remainingLength = length;
private int _chunkIndex;
private int _charIndex;

Expand Down Expand Up @@ -413,7 +419,7 @@ public override int Read(char[] buffer, int index, int count)
return -1;
}

var destination = buffer.AsSpan(index);
var destination = buffer.AsSpan(index, count);
var charsWritten = 0;

var page = _page;
Expand Down Expand Up @@ -443,38 +449,29 @@ public override int Read(char[] buffer, int index, int count)
}
}

var endOfChunkWritten = true;

// Are we about to write past the end of the buffer? If so, adjust source.
// This will be the last chunk we write, so be sure to update charIndex.
if (source.Length > destination.Length)
{
source = source[..destination.Length];
charIndex += source.Length;

// There's more to this chunk to write! Note this so that we don't update
// chunkIndex later.
endOfChunkWritten = false;
}
else
{
chunkIndex++;
charIndex = 0;
}

source.CopyTo(destination);
destination = destination[source.Length..];

charsWritten += source.Length;

// Be careful not to increment chunkIndex unless we actually wrote to the end of the chunk.
if (endOfChunkWritten)
{
chunkIndex++;
}

// Break if we are done writing. chunkIndex and charIndex should have their correct values at this point.
if (destination.IsEmpty)
{
break;
}

charIndex = 0;
}

if (destination.IsEmpty)
Expand Down