System.IO: Fix StringStream reads with empty encoder fallback - #131904
Open
jozkee wants to merge 2 commits into
Open
System.IO: Fix StringStream reads with empty encoder fallback#131904jozkee wants to merge 2 commits into
jozkee wants to merge 2 commits into
Conversation
Continue streaming conversion when the encoder consumes characters without producing bytes so Read does not signal EOF prematurely. Add synchronous, asynchronous, ReadByte, fast-path, and all-skipped-input coverage. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes System.IO.StringStream.Read to avoid returning 0 (EOF) when the encoder consumed input characters but produced zero bytes (e.g., UTF-8 with an empty EncoderReplacementFallback), ensuring reads continue until bytes are produced or the encoder makes no progress. This aligns Read / ReadAsync / ReadByte behavior with expected stream semantics where 0 indicates end-of-stream.
Changes:
- Update
StringStream.Read(Span<byte>)to loop when no bytes have been produced yet but the encoder consumed characters, preventing premature EOF. - Add targeted tests covering sync/async reads,
ReadByte, fast-path vs 1-byte streaming path, and “all input skipped” cases under empty encoder replacement fallback.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/IO/StringStream.cs | Adjusts the streaming conversion loop to retry when characters were consumed but no bytes were produced, avoiding false EOF. |
| src/libraries/System.Runtime/tests/System.IO.Tests/StringStream/StringStreamTests_String.cs | Adds regression tests for empty encoder replacement fallback scenarios across sync/async paths and ReadByte. |
ViveliDuCh
approved these changes
Aug 6, 2026
ViveliDuCh
left a comment
Member
There was a problem hiding this comment.
LGTM, thanks for catching this!
jozkee
enabled auto-merge (squash)
August 6, 2026 15:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
StringStream.Readso it does not return0while encoded input remains after an encoder consumes characters without producing bytes.With UTF-8 and an empty encoder replacement fallback, invalid UTF-16 characters can be consumed while producing no output. The small-buffer streaming path previously interpreted that zero-byte conversion as EOF even when later characters could still produce bytes.
The streaming path now continues conversion while:
It stops after producing output or if the encoder makes no progress. The existing single-shot fast path and async delegation remain unchanged. This follows the retry-until-output-or-EOF precedent used by
TranscodingStream.Tests
Added coverage for:
ReadByte, andValidation
.\build.cmd clr+libs -rc checked— succeeded, 0 warnings, 0 errors.\build.cmd clr.corelib+clr.nativecorelib+libs.pretest -rc checked— succeeded, 0 warnings, 0 errorsdotnet build src\libraries\System.Runtime\tests\System.IO.Tests\System.IO.Tests.csproj /t:Test '/p:XUnitOptions=-class System.IO.Tests.StringStreamTests_String_Misc'— 16 passed, 0 faileddotnet build src\libraries\System.Runtime\tests\System.IO.Tests\System.IO.Tests.csproj /t:Test /p:testnobuild=true— 2,424 passed, 0 failedNote
This pull request description was generated with GitHub Copilot.