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

Fix NSNumber.objCType crashes, make it returns same values with darwin platform version #1025

Closed
wants to merge 10 commits into from

Conversation

norio-nomura
Copy link
Contributor

Fixes SR-4907

  • Add TestNSNumber.test_objCType() reproducing crash
    Expected results are sampled from macOS
  • Fix crash on accessing objCType property of NSNumber

Change NSNumber.objCType to returning same values with darwin platform version

  • Change NSNumber.objCType to returning same values with darwin
  • Change NSNumber.init(value: Bool) to returning __NSCFBoolean by using _Factory protocol

Since NSNumber.objCType returns c with both of Bool and Int8 on darwin platform, NSNumber.init(value: Bool) is changed to returns __NSCFBoolean by using _Factory protocol, and it changed to use _cfTypeID to distinguish between Bool and Int8.

  • Change JSONSerialization to use _cfTypeID for checking whether NSNumber is Boolean or not
  • Change NSNumber.compare(_:) to use objCType
  • Add missing entry to TestNumber.allTests
  • Fix NSKeyedUnarchiver.decodeBool(forKey:) and add test to TestNSPredicate.test_NSCoding()

Expected results are sampled from macOS
…itectures

- Results of `NSNumber.objCType.pointee` depends on architecture if `NSNumber` is initialized with `Int` or `UInt`
- When `NSNumber` is initialized with 64-bit integer, The value of `NSNumber.objCType.pointee` switches from 'q' to 'Q' with `Int64.max` as the boundary.
@norio-nomura
Copy link
Contributor Author

Reflected differences of NSNumber.objCType's result by value and architectures.

@parkera parkera requested a review from phausler June 7, 2017 23:46
@alblue
Copy link
Contributor

alblue commented Jun 9, 2017

@swift-ci please test

@@ -78,7 +78,7 @@ internal class __NSCFBoolean : NSNumber {
override var objCType: UnsafePointer<Int8> {
// This must never be fixed to be "B", although that would
// cause correct old-style archiving when this is unarchived.
return UnsafePointer<Int8>(bitPattern: UInt(_NSSimpleObjCType.Char.rawValue.value))!
return String(_NSSimpleObjCType.Char.rawValue)._bridgeToObjectiveC().utf8String!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to cause leaks, can we have a static allocation somewhere stashed with the type to cstrings?


extension _Factory {
init(factory: () -> Self) {
self = factory()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this is rather strange, if this works perhaps we should investigate using this pattern further

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, just prototyped a test for this... wow! this will solve a ton of issues with swift-corelibs-foundation!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -154,3 +154,14 @@ open class NSValue : NSObject, NSCopying, NSSecureCoding, NSCoding {
}
}

extension NSValue : _Factory {}

protocol _Factory {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for maintainability; can we annotate this as internal?

@norio-nomura
Copy link
Contributor Author

Superseded by #1093

@norio-nomura norio-nomura deleted the nn-fix-sr-4907 branch July 2, 2017 14:34
@norio-nomura
Copy link
Contributor Author

@phausler Thanks for reviews. Those are addressed on #1093.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants