Skip to content
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

FileStream example bug #4141

Closed
Tracked by #64596
malghabr opened this issue Apr 20, 2020 · 3 comments · Fixed by #8560
Closed
Tracked by #64596

FileStream example bug #4141

malghabr opened this issue Apr 20, 2020 · 3 comments · Fixed by #8560
Labels
area-System.IO help wanted Good for community contributors to help [up-for-grabs] Pri3 Indicates issues/PRs that are low priority
Milestone

Comments

@malghabr
Copy link

Hi,

Not sure if this is considered a bug but the first example on the FileStream docs only works if you have a file with less than 1024 bytes or bytes%1024 == 0. This is due to the buffer byte array being reused.
If you use a 4 byte array and try read the string "hello" you end up with the following in the buffer
loop iteration 0: hell
loop iteration 1: oell

hell
oell
This is because the byte array holds data from the first loop
https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.8

@dotnet-bot dotnet-bot added the untriaged New issue has not been triaged by the area owner label Apr 20, 2020
@PRMerger12 PRMerger12 added the Pri3 Indicates issues/PRs that are low priority label Nov 11, 2020
@GSPP
Copy link

GSPP commented Feb 5, 2022

What a weird example anyway. So random, what's the idea behind it? Reminds me of the socket documentation.

@jozkee
Copy link
Member

jozkee commented Mar 10, 2022

Yes, it is a bit silly. Feel free to send a PR to propose an improved snippet.

Here's where you can find the snippets of the current ones:

:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR/FStream Class/CPP/fstream class.cpp" id="Snippet1":::
:::code language="csharp" source="~/snippets/csharp/System.IO/FileStream/Overview/fstream class.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR/FStream Class/VB/fstream class.vb" id="Snippet1":::

@jozkee jozkee added help wanted Good for community contributors to help [up-for-grabs] and removed untriaged New issue has not been triaged by the area owner labels Mar 10, 2022
@malghabr
Copy link
Author

Sure I can pick this up when i have time

@jeffhandley jeffhandley added this to the Backlog milestone Aug 9, 2022
Kissaki added a commit to Kissaki/dotnet-api-docs that referenced this issue Oct 22, 2022
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
Kissaki added a commit to Kissaki/dotnet-api-docs that referenced this issue Oct 22, 2022
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
Kissaki added a commit to Kissaki/dotnet-api-docs that referenced this issue Oct 22, 2022
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
adamsitnik pushed a commit that referenced this issue Oct 24, 2022
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO help wanted Good for community contributors to help [up-for-grabs] Pri3 Indicates issues/PRs that are low priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants