Skip to content

[5.5] Keep the buffer in the invalid UTF8 handling path of String(unsafeUninitializedCapacity:initializingWith:) alive while we're using it #39111

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
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions stdlib/public/core/StringCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ extension String {
)
return result.asString
case .error(let initialRange):
defer { _fixLifetime(result) }
//This could be optimized to use excess tail capacity
return repairUTF8(result.codeUnits, firstKnownBrokenRange: initialRange)
}
Expand Down
37 changes: 24 additions & 13 deletions test/stdlib/StringCreate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,33 @@ if #available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) {
}
expectEqual(expected, actual)
}

let validUTF8: [UInt8] = [0x43, 0x61, 0x66, 0xC3, 0xA9]
let invalidUTF8: [UInt8] = [0x43, 0x61, 0x66, 0xC3]

let cafe1 = String(unsafeUninitializedCapacity: validUTF8.count) {
_ = $0.initialize(from: validUTF8)
return validUTF8.count
}
expectEqual("Café", cafe1)

let cafe2 = String(unsafeUninitializedCapacity: invalidUTF8.count) {
_ = $0.initialize(from: invalidUTF8)
return invalidUTF8.count
let longerValidUTF8: [UInt8] = [0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x43, 0x61, 0x66, 0xC3, 0xA9]
let longerInvalidUTF8: [UInt8] = [0x21, 0x21, 0x43, 0x61, 0x66, 0xC3, 0x43, 0x61, 0x66, 0xC3, 0x43, 0x61, 0x66, 0xC3]

func test(bufferSize: Int, input: [UInt8], expected: String) {
let strs = (0..<100).map { _ in
String(unsafeUninitializedCapacity: bufferSize) { buffer in
_ = buffer.initialize(from: input)
return input.count
}
}
for str in strs {
expectEqual(expected, str)
}
}
expectEqual("Caf�", cafe2)


test(bufferSize: validUTF8.count, input: validUTF8, expected: "Café")
test(bufferSize: invalidUTF8.count, input: invalidUTF8, expected: "Caf�")
// Force non-smol strings by using a larger capacity
test(bufferSize: 16, input: validUTF8, expected: "Café")
test(bufferSize: 16, input: invalidUTF8, expected: "Caf�")
test(bufferSize: longerValidUTF8.count, input: longerValidUTF8, expected: "!!!!!!!!!!Café")
test(bufferSize: longerInvalidUTF8.count, input: longerInvalidUTF8, expected: "!!Caf�Caf�Caf�")
test(bufferSize: 16, input: longerValidUTF8, expected: "!!!!!!!!!!Café")
test(bufferSize: 16, input: longerInvalidUTF8, expected: "!!Caf�Caf�Caf�")
let empty = String(unsafeUninitializedCapacity: 16) { _ in
// Can't initialize the buffer (e.g. the capacity is too small).
return 0
Expand Down