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-10726] Overflow of struct containing variable array #53123

Closed
swift-ci opened this issue May 21, 2019 · 3 comments
Closed

[SR-10726] Overflow of struct containing variable array #53123

swift-ci opened this issue May 21, 2019 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10726
Radar rdar://problem/50987022
Original Reporter allenhsu (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

MacOS 10.14.4

Xcode 10.2.1 (10E1001)

Apple Swift version 5.0.1 (swiftlang-1001.0.82.4 clang-1001.0.46.5)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Miscompile, OptimizedOnly
Assignee None
Priority Medium

md5: dea0191cc38d4e2a231713086830d92b

Issue Description:

We encountered a weird reproducible crash after we submitted a minor bug fix version of our app with latest Xcode. The only change may affect seems to be Xcode and Swift version.

So I tried to make this minimal project to reproduce the issue:

https://github.com/allenhsu/SwiftStructOverflowDemo

The main code is all in ViewController.swift

class ViewController: UIViewController {
    private struct Curve {
        var points = [CGPoint]()
    }
    
//// Solution 1
//    private class Curve {
//        var points = [CGPoint]()
//    }
    
    private var curve: Curve?

    override func viewDidLoad() {
        super.viewDidLoad()
        step1()
        step2()
    }

    func step1() {
        curve = Curve()
        curve?.points = [CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 1)]
        
//// Solution 2
//        var curve = Curve()
//        curve.points = [CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 1)]
//        self.curve = curve
    }
    
    func step2() {
        if let curve = curve {
            print("total points in dashpoints: \(curve.points.count)")
            print("points: \(curve.points)")
        }
    }
}

I think it's straightforward and self-explaining, the issue is that in the `step2` method, the curve.points become invalid, so curve.points.count become an arbitrary large number, and if you iterate over it, it'll crash.

Although I know how to quick fix / work around this issue, I think this may be a issue introduced by the compiler, because:

  1. If I turn "Optimization Level" of "Swift Compiler - Code Generation" to "No Optimization", the output is correct. That's why the issue didn't happen in development environment. Maybe that's also the direction to look into, the optimization part.

  2. The issue can also be resolved if I change the step1 method into:

    func step1() {
        var curve = Curve()
        curve.points = [CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 1)]
        self.curve = curve
    }

Also, if I set break point in step1 method, the curve and its points is valid. So the second guess is it may be also related to live cycle and scope of variables when being optimized.

All that said, I'm not 100% percent sure if it's a but of the compiler, or there's anything I'm doing wrong.

@swift-ci
Copy link
Collaborator Author

Comment by Allen Hsu (JIRA)

I've updated the original code to get rid of the custom view, because after some tests I believe it's not related to UIKit, e.g. draw cycle, etc..

@belkadan
Copy link
Contributor

Yikes![]( Definitely looks like a compiler bug—simple structs like Curve don't have the kind of lifecycle problems you're worried about. Thanks for filing)

@eeckstein
Copy link
Member

This is a duplicate of https://bugs.swift.org/browse/SR-10444

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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 optimized only Flag: An issue whose reproduction requires optimized compilation
Projects
None yet
Development

No branches or pull requests

3 participants