Skip to content

Commit accc9e6

Browse files
gribozavrphausler
authored andcommitted
Fix crash in NSValue.isEqual() when it is passed an NSNumber (#619)
NSNumbers are not added to the side table, so we can't find them.
1 parent 0dc0dac commit accc9e6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Foundation/NSValue.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ open class NSValue : NSObject, NSCopying, NSSecureCoding, NSCoding {
5858
} else {
5959
// bypass _concreteValue accessor in order to avoid acquiring lock twice
6060
let (lhs, rhs) = NSValue.SideTableLock.synchronized {
61-
return (NSValue.SideTable[ObjectIdentifier(self)]!,
62-
NSValue.SideTable[ObjectIdentifier(object)]!)
61+
return (NSValue.SideTable[ObjectIdentifier(self)],
62+
NSValue.SideTable[ObjectIdentifier(object)])
63+
}
64+
if let lhs = lhs, let rhs = rhs {
65+
return lhs.isEqual(rhs)
6366
}
64-
return lhs.isEqual(rhs)
6567
}
6668
}
6769
return false

TestFoundation/TestNSValue.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TestNSValue : XCTestCase {
2929
( "test_valueWithShortArray", test_valueWithShortArray ),
3030
( "test_valueWithULongLongArray", test_valueWithULongLongArray ),
3131
( "test_valueWithCharPtr", test_valueWithULongLongArray ),
32+
( "test_isEqual", test_isEqual ),
3233
]
3334
}
3435

@@ -126,4 +127,11 @@ class TestNSValue : XCTestCase {
126127
NSValue(bytes: &charPtr, objCType: "*").getValue(&expectedPtr)
127128
XCTAssertEqual(charPtr, expectedPtr)
128129
}
130+
131+
func test_isEqual() {
132+
let number = NSNumber(value: Int(123))
133+
var long: Int32 = 123456
134+
let value = NSValue(bytes: &long, objCType: "l")
135+
XCTAssertFalse(value.isEqual(number))
136+
}
129137
}

0 commit comments

Comments
 (0)