Skip to content
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

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

Merged
merged 2 commits into from Aug 16, 2023

Conversation

benrimmington
Copy link
Collaborator

Resolves #57911

@benrimmington
Copy link
Collaborator 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
Collaborator 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
Collaborator 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.

@glessard
Copy link
Contributor

@swift-ci please test

@benrimmington benrimmington merged commit 98ada1b into apple:main Aug 16, 2023
5 checks passed
@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