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

ConcurrentStack.PushRange(T[]) throws an ArgumentOutOfRangeException on empty T[] #62121

Closed
megazyz opened this issue Nov 29, 2021 · 3 comments · Fixed by #62126
Closed

ConcurrentStack.PushRange(T[]) throws an ArgumentOutOfRangeException on empty T[] #62121

megazyz opened this issue Nov 29, 2021 · 3 comments · Fixed by #62126
Assignees
Labels
area-System.Collections enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@megazyz
Copy link

megazyz commented Nov 29, 2021

Description

ConcurrentStack<T>.PushRange(T[]) throws an ArgumentOutOfRangeException when T[] is an empty array. The exception message is 'The startIndex argument must be greater than or equal to zero.'.

  • This behavior is not documented
  • It should do nothing instead of throwing an exception, like List<T>.AddRange(IEnumerable<T>) does

Reproduction Steps

new ConcurrentStack<int>().PushRange(Array.Empty<int>());

Expected behavior

It should not throw an exception.

Actual behavior

It throws an ArgumentOutOfRangeException: 'The startIndex argument must be greater than or equal to zero.'

Regression?

No response

Known Workarounds

Check the array for Length > 0 before calling ConcurrentStack<T>.PushRange(T[]).

Configuration

.NET 6.0.100

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Collections untriaged New issue has not been triaged by the area owner labels Nov 29, 2021
@ghost
Copy link

ghost commented Nov 29, 2021

Tagging subscribers to this area: @dotnet/area-system-collections
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

ConcurrentStack<T>.PushRange(T[]) throws an ArgumentOutOfRangeException when T[] is an empty array. The exception message is 'The startIndex argument must be greater than or equal to zero.'.

  • This behavior is not documented
  • It should do nothing instead of throwing an exception, like List<T>.AddRange(IEnumerable<T>) does

Reproduction Steps

new ConcurrentStack<int>().PushRange(Array.Empty<int>());

Expected behavior

It should not throw an exception.

Actual behavior

It throws an ArgumentOutOfRangeException: 'The startIndex argument must be greater than or equal to zero.'

Regression?

No response

Known Workarounds

Check the array for Length > 0 before calling ConcurrentStack<T>.PushRange(T[]).

Configuration

.NET 6.0.100

Other information

No response

Author: megazyz
Assignees: -
Labels:

area-System.Collections, untriaged

Milestone: -

@martincostello
Copy link
Member

Looks like the argument validation happens before the zero length special case:

ValidatePushPopRangeInput(items, startIndex, count);
// No op if the count is zero
if (count == 0)
return;

which throws because startIndex and items.Length are equal:

int length = items.Length;
if (startIndex >= length || startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ConcurrentStack_PushPopRange_StartOutOfRange);
}

martincostello added a commit to martincostello/runtime that referenced this issue Nov 29, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Nov 29, 2021
@martincostello
Copy link
Member

Proposed fix: #62126

@eiriktsarpalis eiriktsarpalis added enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels Nov 29, 2021
@eiriktsarpalis eiriktsarpalis added this to the 7.0.0 milestone Nov 29, 2021
eiriktsarpalis pushed a commit that referenced this issue Nov 30, 2021
* No-op ConcurrentStack.PushRange(T[]) if empty

Resolves #62121.

* Add more bounds checks

Assert that ArgumentOutOfRangeException is thrown if specifying indexes for an empty array with PushRange(T[], int, int).

* Review feedback

Allow out-of-bounds index on empty array if count is zero.

* Add extra test case

Add a further test case for a non-empty array but with a count parameter value of zero.
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Nov 30, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Dec 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants