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-9627] Reassigning a list of dictionaries causes EXC_BAD_ACCESS on iOS when running the app in release build configuration #52073

Closed
swift-ci opened this issue Jan 9, 2019 · 9 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software optimized only Flag: An issue whose reproduction requires optimized compilation run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jan 9, 2019

Previous ID SR-9627
Radar rdar://problem/47153896
Original Reporter sv-christian (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 10.1 (10B61)

Swift 4.2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, OptimizedOnly, RunTimeCrash
Assignee @eeckstein
Priority Medium

md5: 6afa60a6d76e2ad376139979372e931d

Issue Description:

When running the following code on a device or a simulator (in release build configuration) the app crashes with "EXC_BAD_ACCESS".

The problem was found by accident, and even though the code should not be written like this, someone else might run into the same (or similar) issue.

Below is a trimmed down version of the original code, which exhibit the problem:

import UIKit

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        save(value: 0)
    }

    func save(value: Double?) {
        var params: [[String : Any]] = [["a": 0]]
        params = [[
            "b": 0,
            "c": value.map(String.init) as Any
            ]]
    }
}

I have tried doing the following modifications to the code, which all make the problem go away:

  1. Changing the type of save()'s `value` parameter

  2. Removing the key "b"

  3. Removing the key "c"

  4. Removing `.map(String.init)` from the key "c"

  5. Moving `value` to a local variable

  6. Setting the initial value of `params` to an empty list `[]`

  7. Changing to `let params` and assign the second value directly

Modifications that does not make a difference:

  1. The type of the value in key "a"

  2. Names of the keys

  3. Removing the cast to `Any` (Will prompt a warning)

I was not able to reproduce the problem in the Xcode playground, but I guess that is because the playground runs in debug mode.

@belkadan
Copy link
Contributor

belkadan commented Jan 9, 2019

@eeckstein, @lorentey, does this sound like anything we've fixed? (I haven't tested it yet.)

@eeckstein
Copy link
Member

I could reproduce this, even locally with:

func save(value: Double?) {
    var params: [[String : Any]] = [["a": 0]]
    params = [[
      "b": 0,
      "c": value.map(String.init) as Any
      ]]
}
save(value: 0)

Unfortunately it's not compilable anymore with top of master:

error: ambiguous use of 'init'
      "c": value.map(String.init) as Any}}

@eeckstein
Copy link
Member

@airspeedswift Are we aware of this source breaking change?

@belkadan
Copy link
Contributor

belkadan commented Jan 9, 2019

The ambiguity for String.init (or really any heavily-overloaded initializer) is not a new issue; we should ignore it in regards to this bug.

@eeckstein
Copy link
Member

OK, so what's the fix for this?

@belkadan
Copy link
Contributor

belkadan commented Jan 9, 2019

`value.map({ String($0) })` will get you equivalent behavior.

@eeckstein
Copy link
Member

Thanks.
It still crashes. We'll take a look

@eeckstein
Copy link
Member

This is in fact a bug in the optimizer. A smaller reproducer is

func save() {
    var params: [[String : Int]] = [["": 0]]
    params = [[ "a": 0 ]]
}
save()

@eeckstein
Copy link
Member

master: #21803
5.0: #21844

@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
This issue was closed.
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. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software optimized only Flag: An issue whose reproduction requires optimized compilation run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

4 participants