Environment
macOS 10.14.6
Build tools from Xcode 11.1
uname -mprsv
Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2\~2/RELEASE_X86_64 x86_64 i386
swiftc --version
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0
Additional Detail from JIRA
| | |
|------------------|-----------------|
|Votes | 0 |
|Component/s | Compiler |
|Labels | Bug |
|Assignee | None |
|Priority | Medium |
md5: 113a2b8f1574fa9e503cde77f5f0d852
Issue Description:
When calling prefix(N) on the string returned from URL.lastPathComponent an NSRangeException is thrown if the string is of length N. This only occurs when built with the -O optimization flag.
Example program:
import Foundation
let url = URL(fileURLWithPath: "/System");
let prefix = url.lastPathComponent.prefix(6)
Stack trace:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSPathStore2 characterAtIndex:]: index (6) beyond bounds (6)'
Environment
macOS 10.14.6 Build tools from Xcode 11.1 uname -mprsv Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2\~2/RELEASE_X86_64 x86_64 i386 swiftc --version Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7) Target: x86_64-apple-darwin18.7.0Additional Detail from JIRA
| | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 113a2b8f1574fa9e503cde77f5f0d852Issue Description:
When calling prefix(N) on the string returned from URL.lastPathComponent an NSRangeException is thrown if the string is of length N. This only occurs when built with the -O optimization flag.
Example program:
import Foundation
let url = URL(fileURLWithPath: "/System");
let prefix = url.lastPathComponent.prefix(6)
Stack trace:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSPathStore2 characterAtIndex:]: index (6) beyond bounds (6)'
First throw call stack:
(
0 CoreFoundation 0x00007fff50ebca7d __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff7b5b7a17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff50ebc8af +[NSException raise:format:] + 201
3 Foundation 0x00007fff5307ee50 -[NSPathStore2 characterAtIndex:] + 109
4 libswiftCore.dylib 0x00007fff7c6fcbd3 $ss11_StringGutsV34foreignErrorCorrectedUTF16CodeUnit2ats6UInt16VSS5IndexV_tFTf4nx_n + 51
5 libswiftCore.dylib 0x00007fff7c6fcc7e $ss11_StringGutsV18foreignScalarAlignySS5IndexVAEFTf4nx_n + 14
6 libswiftCore.dylib 0x00007fff7c67a069 $ss11_StringGutsV18foreignScalarAlignySS5IndexVAEF + 9
7 string-test 0x000000010006ca63 main + 323
8 libdyld.dylib 0x00007fff7cd853d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException