Skip to content

Commit 178b9f0

Browse files
Max Moiseevmoiseev
authored andcommitted
[stdlib] Adding bounds check in a.subscript(Index) fast path
UnsafeBufferPoiunter subscript used in the fast path only checks bounds in Debug mode, therefore extra checks are needed. Addresses: <rdar://problem/31992473>
1 parent 5baa080 commit 178b9f0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

stdlib/public/core/StringCharacterView.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,14 @@ extension String.CharacterView : BidirectionalCollection {
445445
let relativeOffset = i._base._position - _coreOffset
446446
if _core.isASCII {
447447
let asciiBuffer = _core.asciiBuffer._unsafelyUnwrappedUnchecked
448-
return Character(UnicodeScalar(asciiBuffer[relativeOffset]))
448+
// Bounds checks in an UnsafeBufferPointer (asciiBuffer) are only
449+
// performed in Debug mode, so they need to be duplicated here.
450+
// Falling back to the non-optimal behavior in the case they don't
451+
// pass.
452+
if relativeOffset >= asciiBuffer.startIndex &&
453+
relativeOffset < asciiBuffer.endIndex {
454+
return Character(UnicodeScalar(asciiBuffer[relativeOffset]))
455+
}
449456
} else if _core._baseAddress != nil {
450457
let cu = _core._nthContiguous(relativeOffset)
451458
// Only constructible if sub-surrogate

validation-test/stdlib/String.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,7 @@ StringTests.test("ForeignIndexes/UnexpectedCrash")
192192
expectEqual("a", acceptor[donor.startIndex])
193193
}
194194

195-
StringTests.test("ForeignIndexes/subscript(Index)/OutOfBoundsTrap")
196-
.skip(.always("<rdar://problem/31992473>"))
197-
.code {
195+
StringTests.test("ForeignIndexes/subscript(Index)/OutOfBoundsTrap") {
198196
let donor = "abcdef"
199197
let acceptor = "uvw"
200198

0 commit comments

Comments
 (0)