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.
diff --git a/Foundation/NSValue.swift b/Foundation/NSValue.swift
index e1c3e66..a8003ae 100644
--- a/Foundation/NSValue.swift
+++ b/Foundation/NSValue.swift
@@ -58,10 +58,12 @@
} else {
// bypass _concreteValue accessor in order to avoid acquiring lock twice
let (lhs, rhs) = NSValue.SideTableLock.synchronized {
- return (NSValue.SideTable[ObjectIdentifier(self)]!,
- NSValue.SideTable[ObjectIdentifier(object)]!)
+ return (NSValue.SideTable[ObjectIdentifier(self)],
+ NSValue.SideTable[ObjectIdentifier(object)])
}
- return lhs.isEqual(rhs)
+ if let lhs = lhs, let rhs = rhs {
+ return lhs.isEqual(rhs)
+ }
}
}
return false
diff --git a/TestFoundation/TestNSValue.swift b/TestFoundation/TestNSValue.swift
index 6d55d6c..d5d4180 100644
--- a/TestFoundation/TestNSValue.swift
+++ b/TestFoundation/TestNSValue.swift
@@ -29,6 +29,7 @@
( "test_valueWithShortArray", test_valueWithShortArray ),
( "test_valueWithULongLongArray", test_valueWithULongLongArray ),
( "test_valueWithCharPtr", test_valueWithULongLongArray ),
+ ( "test_isEqual", test_isEqual ),
]
}
@@ -126,4 +127,11 @@
NSValue(bytes: &charPtr, objCType: "*").getValue(&expectedPtr)
XCTAssertEqual(charPtr, expectedPtr)
}
+
+ func test_isEqual() {
+ let number = NSNumber(value: Int(123))
+ var long: Int32 = 123456
+ let value = NSValue(bytes: &long, objCType: "l")
+ XCTAssertFalse(value.isEqual(number))
+ }
}