You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An NSRangeException arises and the application crashes when getting substring with a Swift range via subscript brackets from a specific Foundation's inner string class instance.
This issue occurs only when a compile optimization is enabled and under macOS 10.14. When runs the code without optimization or under macOS 10.15, it works correctly (I haven't tried macOS 10.13 or other environments yet).
Furthermore, if the same code is written in Objective-C it works even under the same conditions. So, something would go wrong with the range translation between Swift <-> ObjC.
Conditions
The conditions I found so far to reproduce this phenomenon are:
The execution environment is macOS 10.14 (not macOS 10.15).
Optimization -O is enabled.
The actual string instance class is not a pure Swift.String or __NSCFString but NSPathStore2 or NSBigMutableString at least.
The range to obtain substring contains the last index.
The range to obtain substring was made from a Foundation method (not like string.startIndex..<string.endIndex).
Obtaining substring with Range<String.Index> via subscript (not PartialRangeFrom<String.Index>).
In addition, the same code works when I used Xcode 10.3 with Swift 5. But I'm not sure from when (from Xcode 11, 11.1, or from Swift 5.1?) it has been broken.
If not satisfy one of those conditions, the code works as excepted without throwing any exception.
Expected Result
Succeed in obtaining substring without any exception.
Steps to Reproduce the Issue
The followings are sample codes to reproduce those issues.
Run it with -O option under macOS 10.14.x.
$swift -Ofoo.swift
It will crash due to an NSRangeException.
Sample code for NSPathStore2
import Foundation
// create NSPathStore2 instanceletstring=NSString(string:"foo").appendingPathComponent("bar")print((string asNSString).className)// -> NSPathStore2letrange= string.range(of:"foo/bar")!
letsubstring=string[range]// -[NSPathStore2 characterAtIndex:]: index (7) beyond bounds (7)print(substring)
Sample code for NSBigMutableString
import Foundation
import AppKit.NSTextStorage
// create NSBigMutableString instance via NSTextStorageletbigString=String(repeating:"a", count:513)// 512+1 for NSBigMutableStringletstring=NSTextStorage(string: bigString).string
print((string asNSString).className)// -> NSBigMutableStringletrange= string.range(of:"a", options:.backwards)!
letsubstring=string[range]// -[NSBigMutableString characterAtIndex:]: Index 513 out of bounds; string length 513'print(substring)
The text was updated successfully, but these errors were encountered:
The primary change in NSPathStore2 between those releases was to support CFStringGetCharactersPtr, so presumably this is something that occurs on the non-contiguous path. I'll see if I can force that to happen on a newer system with a different string subclass.
Environment
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0
macOS: 10.14.6 (18G95)
Xcode: 11.1 (11A1027)
Additional Detail from JIRA
md5: aa68f9cc8ccaab9b757ab0270295576d
Issue Description:
Description
An
NSRangeException
arises and the application crashes when getting substring with a Swift range via subscript brackets from a specific Foundation's inner string class instance.This issue occurs only when a compile optimization is enabled and under macOS 10.14. When runs the code without optimization or under macOS 10.15, it works correctly (I haven't tried macOS 10.13 or other environments yet).
Furthermore, if the same code is written in Objective-C it works even under the same conditions. So, something would go wrong with the range translation between Swift <-> ObjC.
Conditions
The conditions I found so far to reproduce this phenomenon are:
The execution environment is macOS 10.14 (not macOS 10.15).
Optimization
-O
is enabled.The actual string instance class is not a pure
Swift.String
or__NSCFString
butNSPathStore2
orNSBigMutableString
at least.The range to obtain substring contains the last index.
The range to obtain substring was made from a Foundation method (not like
string.startIndex..<string.endIndex
).Obtaining substring with
Range<String.Index>
via subscript (notPartialRangeFrom<String.Index>
).In addition, the same code works when I used Xcode 10.3 with Swift 5. But I'm not sure from when (from Xcode 11, 11.1, or from Swift 5.1?) it has been broken.
If not satisfy one of those conditions, the code works as excepted without throwing any exception.
Expected Result
Succeed in obtaining substring without any exception.
Steps to Reproduce the Issue
The followings are sample codes to reproduce those issues.
Run it with
-O
option under macOS 10.14.x.It will crash due to an NSRangeException.
Sample code for NSPathStore2
Sample code for NSBigMutableString
The text was updated successfully, but these errors were encountered: