Skip to content

Fix aligned buffer write #11861

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 3 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 testWriter = CodeWriter.GetTestTextReader(pages);
var output = new char[FirstLine.Length];

testWriter.Read(output, 0, output.Length);
Assert.Equal(output, FirstLine.AsSpan());
Array.Clear(output);

testWriter.Read(output, 0, output.Length);
Assert.Equal(output, FirstLine.AsSpan());
Array.Clear(output);

testWriter.Read(output, 0, output.Length);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified that without the charIndex change, this line fails.

Assert.Equal(output, "Second\0\0\0\0".AsSpan());
}

[Fact]
public void ReaderOnlyReadsAsMuchAsRequested()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue is not directly related to the main bug, but is something that @jaredpar noticed when just looking over the code.

{
var pages = new LinkedList<ReadOnlyMemory<char>[]>();

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

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

testWriter.Read(output, 0, 2);
Assert.Equal(output, "Fi\0\0\0\0\0\0\0\0".AsSpan());
Array.Clear(output);

testWriter.Read(output, 0, output.Length);
Assert.Equal(output, "rst Line\0\0".AsSpan());
}
}
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 @@ -466,15 +472,14 @@ public override int Read(char[] buffer, int index, int count)
if (endOfChunkWritten)
{
chunkIndex++;
charIndex = 0;
}

// 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
Loading