[iOS18] Crash in `static NSDecimal./ infix(_:_:)`

          Crashed: com.apple.main-thread
0  Foundation                     0x2bd25c specialized static NSDecimal._integerDivide(dividend:divisor:maxResultLength:) + 1996
1  Foundation                     0x2c1c0c specialized NSDecimal._divide(by:roundingMode:) + 2432
2  Foundation                     0x372710 static NSDecimal./ infix(_:_:) + 64
          Crashed: com.um.positions.fiber.queue
0  Foundation                     0x2bdc94 <redacted> + 1996
1  Foundation                     0x2c2644 <redacted> + 2432
2  Foundation                     0x372804 $sSo9NSDecimala10FoundationE1doiyA2B_ABtFZ + 64

After iOS18, some new crashes appeared, but they never appeared before iOS18.

How to avoid such crashes?

I checked the API documentation and there is no relevant modification, https://developer.apple.com/documentation/foundation/1409398-nsdecimaldivide

However, I parsed iOS_22A5326g/../Foundation.framework/Foundation, and found that this EXC_BREAKPOINT is newly added

You can reproduce it with the following code:

let a: Decimal = .greatestFiniteMagnitude
let b: Decimal = .greatestFiniteMagnitude
let c = a / b // if < iOS18, c = 1. if = iOS18, will crashed ;<
print(c)
Answered by DTS Engineer in 804332022
something must have changed in the inner workings of Decimal.

Right. Foundation underwent significant changes in iOS 18, as part of the unification of Swift and traditional Foundation. You can learn more about this in What’s next for Foundation.

The bad news is that such a large rewrite will inevitably introduce some problems. The good news is that the public Foundation code is now much closer to the code in iOS itself. And in this case, you can see where this trap is coming from, namely an arithmetic overflow in the _integerDivide(…) function.

Note I’m linking to the top of the function, rather than the exact line, because working out the exact line is tricky.

I recommend that you file a bug about this. Please post your bug number, just for the record.

As to what you can do to work around it, that kinda depends on what you’re using Decimal for. IMO it’s something that’s wildly overused, but there are other points of view (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I experience the same crash starting with iOS 18.0 (including 18.1.0). Based on this report (https://github.com/apple/swift-foundation/issues/833), something must have changed in the inner workings of Decimal.

0 Foundation <redacted> + 1996
1 Foundation <redacted> + 2432
2 Foundation $sSo9NSDecimala10FoundationE1doiyA2B_ABtFZ + 64
something must have changed in the inner workings of Decimal.

Right. Foundation underwent significant changes in iOS 18, as part of the unification of Swift and traditional Foundation. You can learn more about this in What’s next for Foundation.

The bad news is that such a large rewrite will inevitably introduce some problems. The good news is that the public Foundation code is now much closer to the code in iOS itself. And in this case, you can see where this trap is coming from, namely an arithmetic overflow in the _integerDivide(…) function.

Note I’m linking to the top of the function, rather than the exact line, because working out the exact line is tricky.

I recommend that you file a bug about this. Please post your bug number, just for the record.

As to what you can do to work around it, that kinda depends on what you’re using Decimal for. IMO it’s something that’s wildly overused, but there are other points of view (-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer
[iOS18] Crash in `static NSDecimal./ infix(_:_:)`
 
 
Q