BinaryInteger.formatted() crashes on big numbers

In a REPL or in an app, this code will crash on the (totally undocumented) formatted() call:

var v: UInt64 = 123
v.formatted() // "123"
v = 13846165101762005916
v.formatted() // Execution interrupted.

( Apple Swift version 5.6.1 (swiftlang-5.6.0.323.66 clang-1316.0.20.12) Target: arm64-apple-macosx12.0)

1 Like

I've done a bit more experimentation and found out exactly at which number it starts crashing: ((2^64 - 1) / 2) + 1 or in swift terms UInt64.max / 2 + 1.

After some research the EXC_BAD_INSTRUCTION that is thrown is most likely the sign of an arithmetic overflow error in swift. Could .formatted use signed integer arithmetic internally?

2 Likes

Good find! Since this has to do with Apple's proprietary framework, not the open-source Foundation, probably best to file a Feedback bug with them.

2 Likes

rdar://problem/FB10031737

1 Like