Skip to content

[5.1] Guard against passing endIndex to foreignScalarAlign when back-deploying to 5.0 stdlibs #27827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions stdlib/public/core/UnicodeHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ extension _StringGuts {
return String.Index(_encodedOffset: idx._encodedOffset)._scalarAligned
}
if _slowPath(self.isForeign) {
// In 5.1 this check was added to foreignScalarAlign, but when this is
// emitted into a client that then runs against a 5.0 stdlib, it calls
// a version of foreignScalarAlign that doesn't check for this, which
// ends up asking CFString for its endIndex'th character, which throws
// an exception. So we duplicate the check here for back deployment.
guard idx._encodedOffset != self.count else { return idx._scalarAligned }

let foreignIdx = foreignScalarAlign(idx)
_internalInvariant_5_1(foreignIdx._isScalarAligned)
return foreignIdx
Expand Down