Skip to content

[stdlib] Fix buffer size of small-capacity strings #67929

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 2 commits into from
Aug 16, 2023

Conversation

benrimmington
Copy link
Contributor

Resolves #57911

@benrimmington
Copy link
Contributor Author

@swift-ci Please smoke test

Copy link
Contributor

@glessard glessard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@@ -243,7 +243,9 @@ extension _SmallString {
fileprivate mutating func withMutableCapacity(
_ f: (UnsafeMutableRawBufferPointer) throws -> Int
) rethrows {
let len = try withUnsafeMutableBytes(of: &_storage, f)
let len = try withUnsafeMutableBytes(of: &_storage) {
try f(.init(rebasing: $0.prefix(_SmallString.capacity)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be justifiable to use .init(_uncheckedStart:count:) here: the preconditions are known to be met.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UMRBP doesn't have an unchecked initializer, so I used init(start:count:) instead.

let smol = try _SmallString(initializingUTF8With: initializer)
let smol = try _SmallString(initializingUTF8With: {
try initializer(.init(rebasing: $0.prefix(capacity)))
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use .init(_uncheckedStart:count:) here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The given capacity could be negative, so I used init(start:count:) instead.

@benrimmington benrimmington requested a review from glessard August 15, 2023 22:42
@glessard
Copy link
Contributor

@swift-ci please test

@benrimmington benrimmington merged commit 98ada1b into swiftlang:main Aug 16, 2023
@benrimmington benrimmington deleted the small-string-57911 branch August 16, 2023 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SR-15615] [SE-0263] Incorrect buffer count for small capacity?
3 participants