Skip to content

Instantly share code, notes, and snippets.

@barnybug
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barnybug/cc7b717387474bea18ac to your computer and use it in GitHub Desktop.
Save barnybug/cc7b717387474bea18ac to your computer and use it in GitHub Desktop.
// Bug demonstrating Release crash in swift code (XCode 6.2 / swift 1.1, Release builds only)
import UIKit
import XCTest
func dictSetValue(value: AnyObject, forKey key: String, inout #dictionary: [String : AnyObject]) {
dictSetValue(value, forKeyPathComponents: key.componentsSeparatedByString("."), dictionary: &dictionary)
}
func dictSetValue(value: AnyObject, forKeyPathComponents components: [String], inout #dictionary: [String : AnyObject]) {
if components.isEmpty {
return
}
let head = components.first!
if components.count == 1 {
dictionary[head] = value
} else {
var child = (dictionary[head] as? [String : AnyObject]) ?? [:]
let tail = Array(components[1..<components.count])
dictSetValue(value, forKeyPathComponents: tail, dictionary: &child)
return dictionary[head] = child // <- crash here
}
}
class SwiftObjectMapperBugTests: XCTestCase {
func testCrash() {
var dict: [String: AnyObject] = [:]
dictSetValue(1, forKey: "a.b", dictionary: &dict)
dictSetValue(2, forKey: "a.c", dictionary: &dict) // <- crash
XCTAssert(true, "crashes before here")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment