swift: Unable To Append Two Emoji Strings On macOS Ventura's Swift

Description Appending two emoji strings together crashes on macOS Ventura

Steps to reproduce

  1. Use macOS Ventura
  2. Run the sample package’s tests or
  3. try to execute the code "⛓".appending("🦑") in the Swift repl

Expected behavior This code should run and execute fine producing a string that is equal to “⛓🦑”

Actual behavior This works correctly on macOS Monterey, but crashes on macOS Ventura with the message Swift/StringUTF16View.swift:144: Fatal error: String index is out of bounds

Environment

  • Swift compiler version info – swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51) – Target: arm64-apple-macosx13.0
  • Xcode version info – Xcode 14.2 – Build version 14C18
  • Deployment target: macOS 11

Sample Project FuzzyMatchingPackage.zip

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 23 (14 by maintainers)

Most upvoted comments

It is a Foundation issue, but it does not involve the code in swift-foundation. I think it’s okay to keep the issue here for now.

@lorentey the tip to use + worked a treat for the reliable crash in our app; thanks!

Re: encoding/locale: note that up-thread there are env -i-based repro recipes that should have isolated the results from the contents of my environment, yet others fail to reproduce the crash. Curious if you know of other places besides the environment where encoding/locale/etc info might be sneaking into the mix (that might differ in your/my systems from those of others’ who have failed to repro the crash).

One very reliable workaround is to avoid calling StringProtocol.appending, preferring to use the Swift native + operator instead. (There are no drawbacks to doing that. Besides investigating the potential stdlib issue, I’ll also try to change the implementation of appending to do the same. However, that change will not apply retroactively, even if it ends up shipping. + does the right thing on all platforms.)

@AnthonyLatsis this is what I see:

$ cat file.swift ; echo; env -i PATH=/usr/bin /usr/bin/swift file.swift
import Foundation
print(NSFoundationVersionNumber)
let _ = "\u{26D3}".appending("\u{1F991}")

1953.3
Swift/StringUTF16View.swift:144: Fatal error: String index is out of bounds
Stack dump:
0.      Program arguments: /Applications/Xcode-14.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -interpret file.swift -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode-14.2.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.1.sdk -color-diagnostics -new-driver-path /Applications/Xcode-14.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode-14.2.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -module-name file -disable-clang-spi -target-sdk-version 13.1
1.      Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
2.      Compiling with the current language version
3.      While running user code "file.swift"
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x0000000104bb35b0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  swift-frontend           0x0000000104bb25b4 llvm::sys::RunSignalHandlers() + 112
2  swift-frontend           0x0000000104bb3c34 SignalHandler(int) + 344
3  libsystem_platform.dylib 0x0000000196c702a4 _sigtramp + 56
4  libswiftCore.dylib       0x00000001a4a20fa0 $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_A2HSus6UInt32VtFySRys5UInt8VGXEfU_yAMXEfU_yAMXEfU_ + 380
5  libswiftCore.dylib       0x00000001a4a20ce4 $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_A2HSus6UInt32VtFySRys5UInt8VGXEfU_yAMXEfU_ + 200
6  libswiftCore.dylib       0x00000001a4a20adc $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_A2HSus6UInt32VtFySRys5UInt8VGXEfU_ + 212
7  libswiftCore.dylib       0x00000001a4a2063c $ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_A2HSus6UInt32VtF + 236
8  libswiftCore.dylib       0x00000001a4a253b0 $sSKsE6_index_8offsetBy5IndexQzAD_SitFSS9UTF16ViewV_Tg5 + 1248
9  libswiftCore.dylib       0x00000001a4c0a084 $sSS9UTF16ViewV15_nativeGetIndex3forSS0E0VSi_tF + 272
10 libswiftCore.dylib       0x00000001a4c05860 $ss15__StringStorageC13getCharacters_5rangeySpys6UInt16VG_So13_SwiftNSRangeatF + 128
11 libswiftCore.dylib       0x00000001a4c059d4 $ss15__StringStorageC13getCharacters_5rangeySpys6UInt16VG_So13_SwiftNSRangeatFTo + 36
12 CoreFoundation           0x0000000196cca740 __CFStringEncodeByteStream + 2708
13 Foundation               0x0000000197bd52d4 -[NSString(NSStringOtherEncodings) getBytes:maxLength:usedLength:encoding:options:range:remainingRange:] + 260
14 Foundation               0x0000000197bda0cc _NSNewStringByAppendingStrings + 440
15 Foundation               0x0000000197bdab14 -[NSString stringByAppendingString:] + 76
16 Foundation               0x0000000197ff921c $sSy10FoundationE9appendingySSqd__SyRd__lF + 148
17 Foundation               0x000000010a2f0118 $sSy10FoundationE9appendingySSqd__SyRd__lF + 18446744071330295696
18 swift-frontend           0x000000010116b998 llvm::orc::runAsMain(int (*)(int, char**), llvm::ArrayRef<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, llvm::Optional<llvm::StringRef>) + 1312
19 swift-frontend           0x0000000100449250 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions const&, swift::SILOptions const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >&&) + 11268
20 swift-frontend           0x00000001001feccc performCompileStepsPostSILGen(swift::CompilerInstance&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 2856
21 swift-frontend           0x0000000100200ce8 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7284
22 swift-frontend           0x00000001001a1294 swift::mainEntry(int, char const**) + 3940
23 dyld                     0x0000000196917e50 start + 2544
Trace/BPT trap: 5