[stdlib][SR-14424] Zero-initialize any unused capacity in '_SmallString.init(initializingUTF8With:)' #36663
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There's a subtle problem with small strings:
Here's why.
The
unsafeUninitializedCapacity
initializer requires users to return the initialized count and to leave the remaining bytes uninitialized. This is what's happening here. To wit:moveInitialize
with overlapping source and destination in this way).All of this works perfectly when the string isn't small. Digging through the sources, I understand that a
_SmallString
's buffer is backed by a tuple of(UInt64, UInt64)
. Although unused bytes are deinitialized here, they aren't zero immediately prior to deinitialization.It has to be the job of
_SmallString
to initialize unused bytes to zero before rebinding the buffer to its original type. The user can't leave the unused capacity zero-initialized themselves after using it because the documentation tells the user that they should leave the unused capacity uninitialized, and therefore leaving it initialized to zero may not be what a non-small string expects.To fix this bug, we simply zero-initialize the unused capacity before rebinding the memory back to the type of
self._storage
.Resolves SR-14424.