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-11259] Subscript-assigning a Data object retrieved from splitting another one crashes the application #3394

Open
ffried opened this issue Aug 5, 2019 · 1 comment

Comments

@ffried
Copy link
Contributor

ffried commented Aug 5, 2019

Previous ID SR-11259
Radar None
Original Reporter @ffried
Type Bug
Environment

Xcode 10.3:
Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)
Target: x86_64-apple-darwin18.7.0

Xcode 11:
Apple Swift version 5.1 (swiftlang-1100.0.266.1 clang-1100.0.32.1)
Target: x86_64-apple-darwin18.7.0

Xcode 12.2:
Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
Target: x86_64-apple-darwin20.1.0

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 263244286731725b695bee9ab16124cf

Issue Description:

The following Swift code crashes at runtime:

import Foundation
extension Data {
   mutating func modify() {
      for (i, byte) in enumerated() {
         switch byte {
         case 0x2D: self[i] = 0x2B // '-' -> '+'
         default: break
         }
      }
   }
}
let data = Data("ey-LCJ1.z7n-unids".utf8)
let parts = data.split(separator: 0x2E) // dot
var mutable = parts[1] // index 0 works
mutable.modify()

Output:

Stack dump:
0.  Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret s5_modifiedDataSlice.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name s5_modifiedDataSlice 
0  swift                    0x0000000104525ee3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x00000001045256bc SignalHandler(int) + 348
2  libsystem_platform.dylib 0x00007fff6e62bb5d _sigtramp + 29
3  libsystem_platform.dylib 0x0000000105aae080 _sigtramp + 2538087744
4  libswiftFoundation.dylib 0x00007fff6df84849 $s10Foundation4DataVys5UInt8VSicis + 9
5  libswiftFoundation.dylib 0x0000000105ae6425 $s10Foundation4DataVys5UInt8VSicis + 2545294309
6  libswiftFoundation.dylib 0x0000000105ae61d4 $s10Foundation4DataVys5UInt8VSicis + 2545293716
7  swift                    0x0000000100dbd50d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 365
8  swift                    0x0000000100dc38e2 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*) + 1090
9  swift                    0x000000010038c6e1 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 58929
10 swift                    0x000000010037a7de swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6862
11 swift                    0x000000010031887e main + 1246
12 libdyld.dylib            0x00007fff6e4403d5 start + 1
fish: 'swift s5_modifiedDataSlice.swift' terminated by signal SIGILL (Illegal instruction)

Update (2020-11-30)

After @belkadan's comment, I've modified the code to use indices instead of enumerated. The crash still occurs:

import Foundation

extension Data {
   mutating func modify() {
      for idx in indices {
         switch self[idx] {
            case 0x2D: self[idx] = 0x2B // '-' -> '+'
            default: break
         }
      }
   }
}

let data = Data("ey-LCJ1.z7n-unids".utf8)
let parts = data.split(separator: 0x2E, maxSplits: 2) // dot
print(parts.count) // -> 2
print(parts.indices) // => 0..<2
var mutable1 = parts[parts.startIndex]
mutable1.modify()
var mutable2 = parts[parts.endIndex]
mutable2.modify()
@belkadan
Copy link

Data's index doesn't always start at 0, so enumerated isn't really what you want. cc @phausler

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants