-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Previous ID | SR-11605 |
Radar | rdar://problem/56226571 |
Original Reporter | 1024jp (JIRA User) |
Type | Bug |
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
Votes | 1 |
Component/s | Foundation |
Labels | Bug |
Assignee | @Catfish-Man |
Priority | Medium |
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.
$swift -O foo.swift
It will crash due to an NSRangeException.
Sample code for NSPathStore2
import Foundation
// create NSPathStore2 instance
let string = NSString(string: "foo").appendingPathComponent("bar")
print((string as NSString).className) // -> NSPathStore2
let range = string.range(of: "foo/bar")!
let substring = string[range] // -[NSPathStore2 characterAtIndex:]: index (7) beyond bounds (7)
print(substring)
Sample code for NSBigMutableString
import Foundation
import AppKit.NSTextStorage
// create NSBigMutableString instance via NSTextStorage
let bigString = String(repeating: "a", count: 513) // 512+1 for NSBigMutableString
let string = NSTextStorage(string: bigString).string
print((string as NSString).className) // -> NSBigMutableString
let range = string.range(of: "a", options: .backwards)!
let substring = string[range] // -[NSBigMutableString characterAtIndex:]: Index 513 out of bounds; string length 513'
print(substring)