-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Added basic implementation for NSString contentsOfURL:usedEncoding: #893
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
Conversation
Foundation/NSString.swift
Outdated
enc?.pointee = String.Encoding.utf8.rawValue | ||
} | ||
|
||
guard let cf = CFStringCreateWithBytes(kCFAllocatorDefault, bytePtr, readResult.length, CFStringConvertNSStringEncodingToEncoding(enc!.pointee), true) else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has a guard here, but uses enc! in the dereferencing of the pointer. It would make sense to have let enc = enc, cf = ... enc, so that if enc isn't passed then we don't blow up the process.
TestFoundation/TestNSString.swift
Outdated
@@ -66,7 +66,8 @@ class TestNSString : XCTestCase { | |||
("test_rangeOfCharacterFromSet", test_rangeOfCharacterFromSet ), | |||
("test_CFStringCreateMutableCopy", test_CFStringCreateMutableCopy), | |||
("test_FromContentsOfURL",test_FromContentsOfURL), | |||
("test_FromContentOfFile",test_FromContentOfFile), | |||
("test_FromContentsOfURLUsedEncodingUTF16BE", test_FromContentsOfURLUsedEncodingUTF16BE), | |||
("test_FromContentOfFile",test_FromContentOfFile), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation looks off by one character on this line.
@swift-ci please test |
In NSString, for contentsOfURL:usedEncoding: checking the BOM of the received data and determinig whether encoding to be used should be UTF 16 BE Fixed guard statement to handle nil case Fixed indentation in test case Updated build.py file to include test input file Fixed Indentation in build.py
@swift-ci please test |
1 similar comment
@swift-ci please test |
If readResult.length is less than 2, unitialized memory will be referenced. And a possible memory leak: cf is created by CFStringCreateWithBytes, but not released when exitting on errors. AFAIK ARC doesn't handle this? |
@zmeyc I thought about that too but there is an else which sets the default encoding to UTF8 and proceed to try decoding the file which could be less than 2 bytes with UTF 8 encoding. |
@mohitathwani Referencing unitialized memory is undefined behavior, it will trigger tools like valgrind, unitialized memory can contain BOM with wrong byte order etc. I propose checking the length before analyzing these two bytes for BOM. |
@swift-ci please test |
Thanks @parkera ! Do you think this is merge worthy? |
Looks good, thanks! |
…L:usedEncoding: This pull request is in continuation to PR swiftlang#893
…L:usedEncoding: This pull request is in continuation to PR swiftlang#893
…L:usedEncoding: This pull request is in continuation to PR swiftlang#893
…L:usedEncoding: This pull request is in continuation to PR swiftlang#893
In NSString, for contentsOfURL:usedEncoding: checking the BOM of the received data and determining whether encoding to be used should be UTF 16 BE or UTF 16 LE