From a1c1779c9f163e703e631a09538c85138ca62561 Mon Sep 17 00:00:00 2001 From: David Smith Date: Fri, 18 Oct 2019 14:24:23 -0700 Subject: [PATCH] Guard against passing endIndex to foreignScalarAlign when back-deploying to 5.0 stdlibs --- stdlib/public/core/UnicodeHelpers.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/stdlib/public/core/UnicodeHelpers.swift b/stdlib/public/core/UnicodeHelpers.swift index b8e0d57cbd40a..64e7ea189fd9f 100644 --- a/stdlib/public/core/UnicodeHelpers.swift +++ b/stdlib/public/core/UnicodeHelpers.swift @@ -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