Skip to content

NSRegularExpression customises the isEqual function but does not customise hashValue and so is unsound #5263

@Lukasa

Description

@Lukasa

The title really explains everything. This program will sometimes crash, and frequently produce silly outcomes:

import Foundation

func main() {
    var s = [NSRegularExpression: Int]()

    let (one, two) = try! (NSRegularExpression(pattern: "foo"), NSRegularExpression(pattern: "foo"))

    print(one.hash)
    print(two.hash)
    print(one == two)

    for _ in 0..<1_000_000 {
        try! s[NSRegularExpression(pattern: "foo")] = 5
    }

    print(s.count)
}

main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions