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

Range<UInt>, Range<Int64>, Range<UInt64> (and ClosedRange) don't trap on overflow

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Standard Library
    • Labels:
      None
    • Environment:

      Xcode 10 beta 1
      Swift version 4.2 (swiftlang-1000.0.16.4 clang-1000.0.25.3)
      macOS 10.13.4 (64 Bit)

      Description

      The expressions (Int.min..<Int.max).count and (Int.min...Int.max).count correctly trap because of an integer overflow. However, the same expressions with UInt, Int64, and UInt64 don't trap. The half-open range versions each return -1, whereas the closed-range versions return 0.

      Half-open ranges:

        1> (UInt.min..<UInt.max).count
      $R0: Int = -1
        2> (Int64.min..<Int64.max).count
      $R1: Int = -1
        3> (UInt64.min..<UInt64.max).count
      $R2: Int = -1
        4> (Int.min..<Int.max).count
      Execution interrupted. Enter code to recover and continue.
      Enter LLDB commands to investigate (type :help for assistance.)
      Process 45232 stopped
      * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
          frame #0: 0x000000010116a752 libswiftCore.dylib`protocol witness for Swift.Strideable.distance(to: A) -> A.Stride in conformance Swift.Int : Swift.Strideable in Swift + 18
      libswiftCore.dylib`protocol witness for Swift.Strideable.distance(to: A) -> A.Stride in conformance Swift.Int : Swift.Strideable in Swift:
      ->  0x10116a752 <+18>: ud2
          0x10116a754 <+20>: ud2
          0x10116a756 <+22>: nopw   %cs:(%rax,%rax)
      
      libswiftCore.dylib`protocol witness for Swift.Strideable.advanced(by: A.Stride) -> A in conformance Swift.Int : Swift.Strideable in Swift:
          0x10116a760 <+0>:  pushq  %rbp
      Target 0: (repl_swift) stopped.
      

      Closed ranges:

        1> (UInt.min...UInt.max).count
      $R0: Int = 0
        2> (Int64.min...Int64.max).count
      $R1: Int = 0
        3> (UInt64.min...UInt64.max).count
      $R2: Int = 0
        4> (Int.min...Int.max).count
      Execution interrupted. Enter code to recover and continue.
      

      I think all of these should trap.

      This is not a regression from Swift 4.1. I'm seeing the same behavior with Xcode 9.4 (Swift version 4.1.2 (swiftlang-902.0.54 clang-902.0.39.2)).

        Attachments

          Activity

            People

            • Assignee:
              moiseev Max Moiseev
              Reporter:
              ole Ole Begemann
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: