Skip to content

Fix snippet buffered read logic - #8560

Merged
adamsitnik merged 1 commit into
dotnet:mainfrom
Kissaki:fix-snippet
Oct 24, 2022
Merged

adamsitnik merged 1 commit into
dotnet:mainfrom
Kissaki:fix-snippet

Conversation

@Kissaki

@Kissaki Kissaki commented Oct 22, 2022 •

Copy link
Copy Markdown
Contributor

The snippet generates a file with 215 bytes.
It reads the file with a 1024 byte buffer.
While this works with a new 0-initialized buffer, the buffered reading misses using the returned read bytes count.
A file with > 1024 bytes will reuse the now non-0 buffer and print unintended values.

Fix buffered reading to be technically correct and work for arbitrary file contents.

Fixes #4141

@Kissaki
Kissaki requested a review from a team as a code owner October 22, 2022 17:58
@ghost ghost added community-contribution Indicates that the PR has been added by a community member area-System.IO labels Oct 22, 2022
@ghost

ghost commented Oct 22, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

The snippet generates a file with 215 bytes.
It reads the file with a 1024 byte buffer.
While this works with a new 0-initialized buffer, the buffered reading misses using the returned read bytes count. A file with > 1024 bytes will reuse the now non-0 buffer and print unintended values.

Fix buffered reading to be technically correct and work for arbitrary file contents.

Fixes #4141

Author: Kissaki
Assignees: -
Labels:

area-System.IO, community-contribution

Milestone: -

The snippet generates a file with 215 bytes.
It reads the file with a 1024 byte buffer.
While this works with a new 0-initialized buffer, the buffered reading misses using the returned read bytes count.
A file with > 1024 bytes will reuse the now non-0 buffer and print unintended values.

Fix buffered reading to be technically correct and work for arbitrary file contents.

Fixes dotnet#4141
@opbld33

opbld33 commented Oct 22, 2022

Copy link
Copy Markdown

Learn Build status updates of commit b017ccc:

✅ Validation status: passed

File Status Preview URL Details
snippets/csharp/System.IO/FileStream/Overview/fstream class.cs ✅Succeeded View

For more details, please refer to the build report.

Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report.

For any questions, please:

@opbld34

opbld34 commented Oct 22, 2022

Copy link
Copy Markdown

Learn Build status updates of commit b054d8c:

✅ Validation status: passed

File Status Preview URL Details
snippets/csharp/System.IO/FileStream/Overview/fstream class.cs ✅Succeeded View

For more details, please refer to the build report.

Note: Broken links written as relative paths are included in the above build report. For broken links written as absolute paths or external URLs, see the broken link report.

For any questions, please:

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thank you @Kissaki !

@adamsitnik

Copy link
Copy Markdown
Member

The CI leg is red due to missing solution file. I've verified manually that the provided code builds fine:

PS D:\projects\repros\docs8560> dotnet build -c Release
MSBuild version 17.4.0-preview-22428-01+14c24b2d3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\7.0.100-rc.1.22431.12\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInferen
ce.targets(219,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-poli
cy [D:\projects\repros\docs8560\docs8560.csproj]
  docs8560 -> D:\projects\repros\docs8560\bin\Release\net7.0\docs8560.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.40

@adamsitnik
adamsitnik merged commit 01cefbe into dotnet:main Oct 24, 2022
@Kissaki
Kissaki deleted the fix-snippet branch June 4, 2024 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileStream example bug

4 participants