Skip to content
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

[SR-9823] String with UTF-16 native representation crashes on using Index created by UTF16View #52240

Open
norio-nomura opened this issue Jan 31, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution standard library Area: Standard library umbrella

Comments

@norio-nomura
Copy link
Contributor

Previous ID SR-9823
Radar None
Original Reporter @norio-nomura
Type Bug
Environment

swift-5.0-DEVELOPMENT-SNAPSHOT-2019-01-29-a

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: 1e493a0e4295a004f68f5fe662199f01

Issue Description:

With following code:

import Foundation

#if !compiler(>=5)
private extension String.UTF8View {
    func withContiguousStorageIfAvailable<R>(_ body: (UnsafeBufferPointer<Element>) throws -> R) rethrows -> R? {
        return nil
    }
}
#endif

extension String {
    var nativeEncoding: Encoding {
        return utf8.withContiguousStorageIfAvailable({$0}) != nil ? .utf8 : .utf16
    }
}

func printUTF16<S: StringProtocol>(_ string: S, label: String) {
    print(label, string.utf16.map { "\\u{\(String($0, radix: 16))}" }.joined())
}

func test(_ string: String) {
    let utf16Index = string.utf16.index(before: string.utf16.endIndex)
    let utf16Before = string[..<utf16Index]
    printUTF16(utf16Before, label: "before:")

    let utf16After = string[utf16Index...]
    printUTF16(utf16After, label: "after:")

    printUTF16(utf16Before + utf16After, label: "joined:")
    print(utf16After.utf8.count)
    print(utf16After.utf16.count)
}

// https://emojipedia.org/family-man-woman-girl-boy/
let string = "\u{1f468}\u{200d}\u{1f469}\u{200d}\u{1f467}\u{200d}\u{1f466}"
print(string.nativeEncoding)
test(string)

let tempfileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
try string.write(to: tempfileURL, atomically: true, encoding: .utf16)
let stringUTF16Backend = try String(contentsOfFile: tempfileURL.path)
print(stringUTF16Backend.nativeEncoding)
test(stringUTF16Backend)

defer { try! FileManager.default.removeItem(at: tempfileURL) }

test() crashes at utf16Before + utf16After with stringUTF16Backend parameter.

log:

$ pbpaste|xcrun --toolchain org.swift.5020190129a swift -
Unicode (UTF-8)
before: \u{d83d}\u{dc68}\u{200d}\u{d83d}\u{dc69}\u{200d}\u{d83d}\u{dc67}\u{200d}\u{d83d}
after: \u{dc66}
joined: \u{d83d}\u{dc68}\u{200d}\u{d83d}\u{dc69}\u{200d}\u{d83d}\u{dc67}\u{200d}\u{d83d}\u{dc66}
4
1
Unicode (UTF-16)
before: \u{d83d}\u{dc68}\u{200d}\u{d83d}\u{dc69}\u{200d}\u{d83d}\u{dc67}\u{200d}\u{d83d}
after: \u{dc66}
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-5.0-DEVELOPMENT-SNAPSHOT-2019-01-29-a.xctoolchain/usr/bin/swift -frontend -interpret - -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name main 
0  swift                    0x0000000112908688 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000112907905 llvm::sys::RunSignalHandlers() + 85
2  swift                    0x0000000112908c92 SignalHandler(int) + 258
3  libsystem_platform.dylib 0x00007fff5a830b5d _sigtramp + 29
4  libsystem_platform.dylib 0x000000000000ffff _sigtramp + 2776495295
5  libswiftCore.dylib       0x0000000118c6def7 $sSS8UTF8ViewV13_foreignIndex5afterSS0D0VAF_tFTf4nx_n + 55
6  libswiftCore.dylib       0x0000000118c6e7d0 $sSKsE9_distance4from2toSi5IndexQz_AEtFSS8UTF8ViewV_Tg5Tf4nnx_n + 96
7  libswiftCore.dylib       0x0000000118c72ac9 $sSS8UTF8ViewV16_foreignDistance4from2toSiSS5IndexV_AGtFTf4nnx_n + 9
8  libswiftCore.dylib       0x0000000118a5afb3 $ss32_copyCollectionToContiguousArrayys0dE0Vy7ElementQzGxSlRzlFSs8UTF8ViewV_Tgq5 + 291
9  libswiftCore.dylib       0x0000000118c73553 $sSS14_fromSubstringySSSshFZTf4nd_n + 1219
10 libswiftCore.dylib       0x0000000118be6769 $sSs6append10contentsOfyx_tSTRzSJ7ElementRtzlF + 73
11 libswiftCore.dylib       0x0000000118be6970 $sSs17UnicodeScalarViewVSmsSm6append10contentsOfyqd__n_tSTRd__7ElementQyd__AERtzlFTWTm + 16
12 libswiftCore.dylib       0x0000000118be6950 $sSsSmsSm6append10contentsOfyqd__n_tSTRd__7ElementQyd__ACRtzlFTW + 16
13 libswiftCore.dylib       0x0000000118b911fd $sSmsE1poiyxx_qd__tSmRd__7ElementQyd__ABRtzlFZ + 125
14 libswiftCore.dylib       0x000000011696b033 $sSmsE1poiyxx_qd__tSmRd__7ElementQyd__ABRtzlFZ + 4259159731
15 libswiftCore.dylib       0x000000011696a49a $sSmsE1poiyxx_qd__tSmRd__7ElementQyd__ABRtzlFZ + 4259156762
16 swift                    0x000000010f47eadd llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 461
17 swift                    0x000000010f482491 llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, 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&, char const* const*) + 1313
18 swift                    0x000000010ed4979b 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&, swift::SILOptions const&) + 3579
19 swift                    0x000000010ed23681 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 13633
20 swift                    0x000000010ed1f0ad swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 3021
21 swift                    0x000000010ecd104e main + 686
22 libdyld.dylib            0x00007fff5a64a3ed start + 1
23 libdyld.dylib            0x000000000000000a start + 2778422302
[1]    12547 done                pbpaste | 
       12548 segmentation fault  xcrun --toolchain org.swift.5020190129a swift -
@belkadan
Copy link
Contributor

cc @milseman

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants