Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ConcurrentStack<T>.TryPopRange does not throw ArgumentOutOfRangeException when startIndex equals the length of items #10508

Copy link
Copy link
Open
@krymtkts

Description

@krymtkts
Issue body actions

Description

ConcurrentStack<T>.TryPopRange method does not throw an ArgumentOutOfRangeException when startIndex is equal to the length of items array.
The documentation is as follows.

ConcurrentStack.TryPopRange Method (System.Collections.Concurrent) | Microsoft Learn

ArgumentOutOfRangeException
startIndex or count is negative. Or startIndex is greater than or equal to the length of items.

This behavior is observed in .NET 8(PowerShell 7.4) but not in .NET Framework 4.5(Windows PowerShell 5.1), where the exception is correctly thrown.

.NET Framework checks whether the startIndex is equal to the length of items.

https://github.com/microsoft/referencesource/blob/51cf7850defa8a17d815b4700b67116e3fa283c2/mscorlib/system/collections/Concurrent/ConcurrentStack.cs#L473C1-L492

.NET doesn't.

https://github.com/dotnet/runtime/blob/48dbc4fc836da67cf1efb6b348499a918c4dea8e/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentStack.cs#L390-L404

Reproduction Steps

Run the following code in PowerShell 7.4(.NET 8).

$s = [System.Collections.Concurrent.ConcurrentStack[System.String]]::new()
$a = [string[]]::new(0)
$s.TryPopRange($a) # TryPopRange(T[] items) calls TryPopRange(items, 0, items.Length)

$s.Push("a")
$b = [string[]]::new(1)
$s.TryPopRange($b, 1, 0)

Expected behavior

According to the documentation, ArgumentOutOfRangeException should be thrown when startIndex is equal to the length of items array.

Actual behavior

No exception is thrown.

Regression?

Yes, this behavior seems to be a regression from Use ArgumentOutOfRangeException.Throw helpers in more places (#79460) · dotnet/runtime@3689fbe.

Known Workarounds

Check startIndex and the length of items manually before calling TryPopRange.

Configuration

.NET 8.0.401

Other information

I noticed this issue when I passed an empty array to TryPopRange.
Although it doesn't match the documentation, the current behavior of not throwing an error when passing an empty array is convenient.
It might be better to implement an early return if the third argument count is 0 before ValidatePushPopRangeInput.

https://github.com/dotnet/runtime/blob/48dbc4fc836da67cf1efb6b348499a918c4dea8e/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentStack.cs#L533-L548

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityIndicates issues/PRs that are low priorityarea-System.Collectionsdocumentationhelp wantedGood for community contributors to help [up-for-grabs]Good for community contributors to help [up-for-grabs]untriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.