Integer overflow gives EXC_BAD_INSTRUCTION in Swift

While communicating with Swift, I noticed that when a 64-bit integer overflows, I get the following error:

file: ///Users/user/Documents/playground/MyPlayground.playground/: error: the execution of the playground was interrupted: the execution was interrupted, reason: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0).

func fibonacci(which: Int) -> (fibOf: Int, isEqualTo: Int) { var i = 1, j = 1 for var k = 2; k < which; k += 1 { let tmp = i + j // this line is highlighted when error occurs j = i i = tmp } return (which, i) } print (fibonacci(92)) print (fibonacci(93)) // this triggers an error 

The first call, i.e. with argument 92 as an argument, will work fine. However, when delivering a value of 93, I get an inappropriate error EXC_BAD_INSTRUCTION. Is this a mistake or what? Usually I expect it to overflow.

+6
source share
1 answer

Expected Behavior. If you want to overflow, you need to use overflow operators .

  • Adding Overflow ( &+ )
  • Subtraction Overflow ( &- )
  • Overflow Multiplication ( &* )
+9
source

All Articles