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-7948] Range<UInt>, Range<Int64>, Range<UInt64> (and ClosedRange) don't trap on overflow #50483

Closed
ole opened this issue Jun 9, 2018 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@ole
Copy link
Contributor

ole commented Jun 9, 2018

Previous ID SR-7948
Radar None
Original Reporter @ole
Type Bug
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)

Additional Detail from JIRA
Votes 1
Component/s Standard Library
Labels Bug
Assignee @moiseev
Priority Medium

md5: ee0a4e5e5dca13901478b4828f3f5e91

Issue 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 #&#8203;1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    frame #&#8203;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)).

@ole
Copy link
Contributor Author

ole commented Mar 27, 2020

Just to give an update, this issue still exists in Swift 5.2.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@ole
Copy link
Contributor Author

ole commented Apr 26, 2022

Verified as fixed in Swift 5.6 (Xcode 13.3.1). I don't know when it was fixed.

@ole ole closed this as completed Apr 26, 2022
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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

1 participant