Skip to content

IndexOutOfRangeException in HashSet.EnsureCapacity method #30698

Closed
dotnet/corefx
#40638

Description

@omariom

SetCapacity assumes hashCode is always positive.

Repro:

    var set = new HashSet<int>();
    set.Add(0);
    set.Add(1);
    set.Remove(0);
    
    set.EnsureCapacity(7);

Activity

  1. omariom commented on Aug 27, 2019

    @omariom
    ContributorAuthor

    Are you seeing an exception thrown?

    Yep

  2. self-assigned this
    on Aug 27, 2019
  3. stephentoub commented on Aug 28, 2019

    @stephentoub
    Member

    It's not that the actual hash codes can be negative. It's that SetCapacity isn't taking the free list into account.

  4. scalablecory commented on Aug 28, 2019

    @scalablecory
    Contributor

    @stephentoub yup, have a fix incoming.

  5. scalablecory commented on Aug 28, 2019

    @scalablecory
    Contributor

    This bug has existed since 2.1 -- it seems nobody is using this method in this way.

  6. scalablecory commented on Aug 28, 2019

    @scalablecory
    Contributor

    The fix is in for .NET 5. Thanks for the report.

  7. danmoseley commented on Aug 28, 2019

    @danmoseley
    Contributor

    Does Dictionary have a test for this?

  8. transferred this issue fromdotnet/corefxon Feb 1, 2020
  9. added this to the 5.0 milestone on Feb 1, 2020
  10. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions