Description
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
.
.NET doesn't.
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
.