Uploaded image for project: 'Swift'
  1. Swift
  2. SR-4393

Miscompilation: object modified after being freed (Swift 3.1 regression)

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Environment:

      Apple Swift version 3.1 (swiftlang-802.0.48 clang-802.0.38)
      Target: x86_64-apple-macosx10.9

      Description

      main.swift

      import Test
      import Foundation
      
      enum T: String {
          case a = "a"
          case b = "b"
      }
      
      func f(_ components: URLComponents) -> [T : String] {
          return components.queryItems?
              .flatMap { item -> (T, String)? in
                  if let parameter = T(rawValue: item.name) {
                      return (parameter, item.value ?? "")
                  } else {
                      return nil
                  }
              }
              .uniqueIndex(allowDuplicateKeys: true) { $0.0 }
              .mapValues { $0.1 }
              ?? [:]
      }
      
      let c: URLComponents = {
          var c = URLComponents()
          c.queryItems = [
              URLQueryItem(name: T.a.rawValue, value: "1"),
              URLQueryItem(name: T.a.rawValue, value: "2"),
              URLQueryItem(name: T.b.rawValue, value: "1"),
              URLQueryItem(name: "test", value: "1")
          ]
          
          return c
      }()
      let x = f(c)
      
      print(x)
      

      lib.swift

      public extension Sequence {
          func uniqueIndex<K: Hashable>(
              allowDuplicateKeys: Bool = false,
              _ key: (Iterator.Element) -> K
          ) -> [K : Iterator.Element] {
              var result: [K : Iterator.Element] = [:]
              
              for item in self {
                  let key = key(item)
                  
                  guard result.updateValue(item, forKey: key) == nil || allowDuplicateKeys else {
                      fatalError("Duplicated key found: \(key)")
                  }
              }
              
              return result
          }
      }
      
      public extension Dictionary {
          func mapValues<NewValue>(_ transformer: (Value) -> NewValue) -> [Key : NewValue] {
              var result = [Key: NewValue](minimumCapacity: self.count)
              
              for (key, value) in self {
                  result[key] = transformer(value)
              }
              
              return result
          }
      }
      

      xcrun -sdk macosx swiftc -module-name Test -emit-module -emit-library -O -wmo lib.swift
      xcrun swiftc -target x86_64-apple-macosx10.10 -sdk $(xcrun --show-sdk-path --sdk macosx) -I "." -L "." -lTest -O main.swift
      

      This is a reduced version of the real crash, which I ran with the address sanitizer, and this is the full output: https://gist.github.com/NachoSoto/8d991d1dec478eb0e1fc4f2b294e2cba.

        Attachments

        1. reduced.swift
          0.6 kB
        2. testrle.out.sil
          26 kB
        3. testrle.sil
          36 kB

          Issue Links

            Activity

              People

              Assignee:
              eeckstein Erik Eckstein
              Reporter:
              NachoSoto Nacho Soto
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: