Skip to content

Commit 612aadd

Browse files
committed
[stdlib] Zero-initialize any unused capacity in '_SmallString.init(initializingUTF8With:)'.
1 parent 2f6fb2d commit 612aadd

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

stdlib/public/core/SmallString.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -286,8 +286,15 @@ extension _SmallString {
286286
// Restore the memory type of self._storage
287287
_ = rawPtr.bindMemory(to: RawBitPattern.self, capacity: 1)
288288
}
289-
return try initializer(
289+
let initializedCount = try initializer(
290290
UnsafeMutableBufferPointer<UInt8>(start: ptr, count: capacity))
291+
// Zero-initialize any unused capacity (which could be deinitialized).
292+
let unusedCapacity = capacity &- initializedCount
293+
if unusedCapacity > 0 {
294+
(rawPtr + initializedCount).initializeMemory(
295+
as: UInt8.self, repeating: 0, count: unusedCapacity)
296+
}
297+
return initializedCount
291298
}
292299
self._invariantCheck()
293300
}

0 commit comments

Comments
 (0)