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

'malloc: [...] pointer being freed was not allocated' during runtime when using 'indirect' enum case with '[Self]' associated value

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Environment:
      • Xcode 10.2 beta 2 (10P91b)
      • Apple Swift version 5.0 (swiftlang-1001.0.60.3 clang-1001.0.37.8)
        Target: x86_64-apple-darwin18.2.0
        ABI version: 0.7
      • macOS 10.14.2 (18C54)

      Description

      The following test case traps during runtime in the latest Xcode 10.2 beta 2 (10P91b):

      public enum MatchRule<U: Equatable> {
          case any
          case equals(U)
          indirect case all([MatchRule])
          
          public func evaluate(_ value: U) -> Bool {
              switch self {
              case .any:
                  return true
                  
              case .equals(let v):
                  return value == v
                  
              case .all(let rules):
                  for r in rules {
                      if !r.evaluate(value) {
                          return false
                      }
                  }
                  
                  return true
              }
          }
      }
      
      print(MatchRule<String>.all([.equals("a"), .any]).evaluate("a"))
      

      The error reads:

      $ ./Test
      Test(4717,0x10f7375c0) malloc: *** error for object 0x7fea28d0bc40: pointer being freed was not allocated
      Test(4717,0x10f7375c0) malloc: *** set a breakpoint in malloc_error_break to debug
      

      Removing the spurious 'indirect' modifier from 'case all([MatchRule])' fixes the crash and the code behaves normally.

        Attachments

          Activity

            People

            Assignee:
            gottesmm Michael Gottesman
            Reporter:
            luiz Luiz Fernando Silva
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: