Skip to content

MemoryStream.ValidatePosition throws when Position == length #3536

@tombarry25

Description

@tombarry25

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: )

Metadata

Metadata

Assignees

Labels

Completed 🔥bug 🐛An unexpected issue that highlights incorrect behaviorhigh-performance 🚂Issues/PRs for the Microsoft.Toolkit.HighPerformance package

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions