Skip to content

Commit 9661dae

Browse files
Fix nasty bug in CodeWriter's TextReader implementation
`CodeWriter.Reader.Read(char[] buffer, int index, int count)` is implemented incorrectly when called after called after it has no more data to return. In that case, it returns -1 rather than 0. Because this method is called in a loop by `TextReader.ReadBlock(...)` while the result is greater than 0, -1 does end the loop. However, It causes -1 to be *added* to the final length returned by `ReadBlock`, reducing it by 1. This causes the `LargeText` returned to have a length that is 1 fewer than expected. In addition, the `TextReader` implementaion failed to reduce `_remainingLength` as expected. This is now fixed as well.
1 parent 0d5fb8c commit 9661dae

File tree

1 file changed

+9
-1
lines changed
  • src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration

1 file changed

+9
-1
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriter.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public override int Read(char[] buffer, int index, int count)
415415

416416
if (_page is null)
417417
{
418-
return -1;
418+
return 0;
419419
}
420420

421421
var destination = buffer.AsSpan(index, count);
@@ -461,6 +461,11 @@ public override int Read(char[] buffer, int index, int count)
461461
charIndex = 0;
462462
}
463463

464+
if (source.IsEmpty)
465+
{
466+
continue;
467+
}
468+
464469
source.CopyTo(destination);
465470
destination = destination[source.Length..];
466471

@@ -497,6 +502,8 @@ public override int Read(char[] buffer, int index, int count)
497502
_charIndex = -1;
498503
}
499504

505+
_remainingLength -= charsWritten;
506+
500507
return charsWritten;
501508
}
502509

@@ -551,6 +558,7 @@ public override string ReadToEnd()
551558
_page = null;
552559
_chunkIndex = -1;
553560
_charIndex = 1;
561+
_remainingLength = 0;
554562

555563
return result;
556564
}

0 commit comments

Comments
 (0)