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

MemoryStream.ValidatePosition throws when Position == length #3536

Closed
4 of 19 tasks
tombarry25 opened this issue Oct 20, 2020 · 6 comments · Fixed by #3543
Closed
4 of 19 tasks

MemoryStream.ValidatePosition throws when Position == length #3536

tombarry25 opened this issue Oct 20, 2020 · 6 comments · Fixed by #3543
Assignees
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior Completed 🔥 high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package
Milestone

Comments

@tombarry25
Copy link

tombarry25 commented Oct 20, 2020

If you read to the end of the MemoryStream, Position == length. If you then try to
call mstrm.Postion = mstrm.Position, it throws an ArgOutOfRangeException

var foo = new byte[10];
var rom = new ReadOnlyMemory<byte>(foo);
using (var stream = rom.AsStream())
{
    for (int i = 0; i < foof.Length; i++)
    {
        var b = new byte[1];
        stream.Read(b, 0, 1);
        Console.WriteLine(stream.Position);
     }
    //Throws out of range exception
     stream.Position = stream.Position;
}

In MemoryStream.cs, should probably be '>' rather than '>=' to be consistent with the behavior of other streams

public static void ValidatePosition(long position, int length)
{
    if ((ulong)position >= (ulong)length)
    {
        ThrowArgumentOutOfRangeExceptionForPosition();
    }
}

NuGet Package(s): microsoft.toolkit.highperformance

Package Version(s): v6.1.1

Windows 10 Build Number:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • May 2020 Update (19041)
  • Insider Build (build number: )

App min and target version:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • May 2020 Update (19041)
  • Insider Build (xxxxx)

Device form factor:

  • Desktop
  • Xbox
  • Surface Hub
  • IoT

Visual Studio

  • 2017 (version: )
  • 2019 (version: )
  • 2019 Preview (version: )
@tombarry25 tombarry25 added the bug 🐛 An unexpected issue that highlights incorrect behavior label Oct 20, 2020
@ghost ghost added the needs triage 🔍 label Oct 20, 2020
@ghost
Copy link

ghost commented Oct 20, 2020

Hello tombarry25, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

@Kyaa-dost
Copy link
Contributor

@tombarry25 Can you please provide detailed information and expected behavior? Screenshots/gifs are highly recommended. Thanks!

@tombarry25
Copy link
Author

tombarry25 commented Oct 21, 2020 via email

@michael-hawker
Copy link
Member

@Sergio0694 this one's for you! 🙂

@michael-hawker michael-hawker added the high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package label Oct 22, 2020
@michael-hawker michael-hawker added this to the 7.0 milestone Oct 22, 2020
@ghost ghost added the In-PR 🚀 label Oct 22, 2020
@Sergio0694
Copy link
Member

Hey @tombarry25, thanks for the detailed issue and repro, and for spotting the bug! 😄

You were absolutely correct, the stream shouldn't throw an exception when seeking to the end.
Added a fix and a new unit test including the repro you provided, along with further validation for other seeking APIs.

@tombarry25
Copy link
Author

tombarry25 commented Oct 22, 2020 via email

@ghost ghost closed this as completed in #3543 Oct 26, 2020
ghost pushed a commit that referenced this issue Oct 26, 2020
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 -->

## Fixes #3536
<!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. -->

<!-- Add a brief overview here of the feature/bug & fix. -->

## PR Type
What kind of change does this PR introduce?
<!-- Please uncomment one or more that apply to this PR. -->

- Bugfix
<!-- - Feature -->
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->


## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
Seeking to the end of a `Stream` from a `Memory<T>` or other types in the `HighPerformance` package throws an exception.

## What is the new behavior?
<!-- Describe how was this issue resolved or changed? -->
Seeking to the end of the stream is now allowed. This is consistent to other `Stream` types in the BCL.

## PR Checklist

Please check if your PR fulfills the following requirements:

- [X] Tested code with current [supported SDKs](../readme.md#supported)
- [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~
- [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~
    - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https:/windows-toolkit/WindowsCommunityToolkit-design-assets)~~
- [X] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [X] Contains **NO** breaking changes
@ghost ghost added Completed 🔥 and removed In-PR 🚀 labels Oct 26, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 25, 2020
Sergio0694 pushed a commit to CommunityToolkit/dotnet that referenced this issue Oct 29, 2021
<!-- 🚨 Please Do Not skip any instructions and information mentioned below as they are all required and essential to evaluate and test the PR. By fulfilling all the required information you will be able to reduce the volume of questions and most likely help merge the PR faster 🚨 -->

## Fixes CommunityToolkit/WindowsCommunityToolkit#3536
<!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. -->

<!-- Add a brief overview here of the feature/bug & fix. -->

## PR Type
What kind of change does this PR introduce?
<!-- Please uncomment one or more that apply to this PR. -->

- Bugfix
<!-- - Feature -->
<!-- - Code style update (formatting) -->
<!-- - Refactoring (no functional changes, no api changes) -->
<!-- - Build or CI related changes -->
<!-- - Documentation content changes -->
<!-- - Sample app changes -->
<!-- - Other... Please describe: -->


## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
Seeking to the end of a `Stream` from a `Memory<T>` or other types in the `HighPerformance` package throws an exception.

## What is the new behavior?
<!-- Describe how was this issue resolved or changed? -->
Seeking to the end of the stream is now allowed. This is consistent to other `Stream` types in the BCL.

## PR Checklist

Please check if your PR fulfills the following requirements:

- [X] Tested code with current [supported SDKs](../readme.md#supported)
- [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~
- [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~
    - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https:/windows-toolkit/WindowsCommunityToolkit-design-assets)~~
- [X] Tests for the changes have been added (for bug fixes / features) (if applicable)
- [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*)
- [X] Contains **NO** breaking changes
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior Completed 🔥 high-performance 🚂 Issues/PRs for the Microsoft.Toolkit.HighPerformance package
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants