mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Swift 5.7 added stronger index validation for `String`, so some illegal cases that previously triggered inconsistently diagnosed out of bounds accesses now result in reliable runtime errors. Similarly, attempts at applying an index originally vended by a UTF-8 string on a UTF-16 string now result in a reliable runtime error. As is usually the case, new traps to the stdlib exposes code that contains previously undiagnosed / unreliably diagnosed coding issues. Allow invalid code in binaries built with earlier versions of the stdlib to continue running with the 5.7 library by disabling some of the new traps based on the version of Swift the binary was built with. In the case of an index encoding mismatch, allow transcoding of string storage regardless of the direction of the mismatch. (Previously we only allowed transcoding a UTF-8 string to UTF-16.) rdar://93379333
30 lines
1.1 KiB
Swift
30 lines
1.1 KiB
Swift
// RUN: %empty-directory(%t)
|
|
// RUN: %target-build-swift -g %s -o %t/StringIndex
|
|
// RUN: %target-codesign %t/StringIndex
|
|
// RUN: env %env-SWIFT_BINARY_COMPATIBILITY_VERSION=0x050600 %target-run %t/StringIndex %S/Inputs/
|
|
|
|
// REQUIRES: executable_test
|
|
// UNSUPPORTED: freestanding
|
|
// UNSUPPORTED: use_os_stdlib || back_deployment_runtime
|
|
// UNSUPPORTED: swift_stdlib_asserts
|
|
|
|
// In 5.7, a number of String APIs started performing stronger index validation,
|
|
// eliminating issues such as out of bounds memory accesses when members are
|
|
// given invalid indices. The environment variable
|
|
// SWIFT_BINARY_COMPATIBILITY_VERSION above forces the stdlib's bincompat
|
|
// version to 5.6 so that we can test older behavior.
|
|
//
|
|
// We can only test old behavior that doesn't lead to undefined behavior,
|
|
// though.
|
|
|
|
import StdlibUnittest
|
|
|
|
var suite = TestSuite("StringIndexBinCompatTests")
|
|
defer { runAllTests() }
|
|
|
|
suite.test("String.index(before:) on an index near the start") {
|
|
let string = "😲 hi"
|
|
let i = string.utf8.index(after: string.utf8.startIndex)
|
|
expectEqual(string.index(before: i), string.startIndex)
|
|
}
|