Possible bug in String.insert?

I've been getting a strange crash only on certain devices (I think possibly 32-bit only) running iOS 9 and iOS 10, when inserting a value into a string. For example, on an iPad Retina running iOS 9.3. I am using the Swift 5 tool chain in Xcode 10.2.1

I've managed to distill down to a simple 4 lines a sequence of steps that will cause the crash:

var stringValue = "     ".appending(" ") // start with 5 spaces, and append a sixth
stringValue.insert(" ", at: stringValue.startIndex) // no crash
stringValue.insert(" ", at: stringValue.startIndex) // no crash
stringValue.insert(" ", at: stringValue.startIndex) // crash with EXC_BAD_ACCESS (code=1, address=0x24)

Interestingly, it is when attempting to grow the string from 8 to 9 characters that the crash happens.

The following code doesn't cause a crash:

var stringValue = "      " // start with 6 spaces
stringValue.insert(" ", at: stringValue.startIndex) // no crash
stringValue.insert(" ", at: stringValue.startIndex) // no crash
stringValue.insert(" ", at: stringValue.startIndex) // no crash

Am I missing something obvious here in how I'm doing this, or is this a genuine crash in the runtime?

1 Like

This should be a valid code. Would you mind filing a bug report?

1 Like