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

[5.8] [stdlib] Fix String.reserveCapacity underallocation #65988

Merged

Conversation

natecook1000
Copy link
Member

This is a cherry pick of #65902 for the 5.8 branch.

When called on a string that is not uniquely referenced, String.reserveCapacity(_:) ignores the current capacity, using the passed-in capacity for the size of its new storage. This can result in an underallocation and write past the end of the new buffer.

This fix changes the new size calculation to use the current UTF-8 count as the minimum. Non-native or non-unique strings now allocate the requested capacity (or space enough for the current contents, if that's larger than what's requested).

rdar://109275875
Fixes #53483

When called on a string that is not uniquely referenced,
`String.reserveCapacity(_:)` ignores the current capacity, using
the passed-in capacity for the size of its new storage. This can
result in an underallocation and write past the end of the new
buffer.

This fix changes the new size calculation to use the current UTF-8
count as the minimum. Non-native or non-unique strings
now allocate the requested capacity (or space enough for the
current contents, if that's larger than what's requested).

rdar://109275875
Fixes apple#53483
@natecook1000 natecook1000 requested a review from a team as a code owner May 17, 2023 22:23
@natecook1000
Copy link
Member Author

@swift-ci Please test

@natecook1000
Copy link
Member Author

cc @apple/swift5-branch-managers

watchOS devices can have different allocation characteristics
from other devices. This modifies the string capacity growth
tests to be more flexible about measuring the growth in
capacity, specifically when more is allocated than requested.
@natecook1000
Copy link
Member Author

@swift-ci Please test

@stephentyrone stephentyrone merged commit 5e352c8 into apple:release/5.8 Jul 25, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants