Skip to content

Commit 0115ffc

Browse files
committed
[stdlib] harden bounds checking in Substring’s span
1 parent a0116a1 commit 0115ffc

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

stdlib/public/core/Substring.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,7 @@ extension Substring.UTF8View {
761761
let base: String.UTF8View = self._base
762762
let first = base._foreignDistance(from: base.startIndex, to: startIndex)
763763
let count = base._foreignDistance(from: startIndex, to: endIndex)
764-
let span = unsafe base.span._extracting(
765-
unchecked: Range(_uncheckedBounds: (first, first &+ count))
766-
)
764+
let span = unsafe base.span._extracting(first..<(first &+ count))
767765
return unsafe _overrideLifetime(span, borrowing: self)
768766
}
769767
#endif
@@ -773,10 +771,8 @@ extension Substring.UTF8View {
773771
fatalError("Span over the small string form is not supported yet.")
774772
}
775773
_internalInvariant(_wholeGuts.isFastUTF8)
776-
let buffer = unsafe _wholeGuts._object.fastUTF8.extracting(
777-
Range(_uncheckedBounds: (first, end))
778-
)
779-
let span = unsafe Span(_unsafeElements: buffer)
774+
var span = unsafe Span(_unsafeElements: _wholeGuts._object.fastUTF8)
775+
span = span._extracting(first..<end)
780776
return unsafe _overrideLifetime(span, borrowing: self)
781777
}
782778
}

0 commit comments

Comments
 (0)