Skip to content

Commit f9fa824

Browse files
committed
[stdlib] Dictionary: Fix getObjects:andKeys:count: implementations
(cherry picked from commit a951cf2)
1 parent c46f73b commit f9fa824

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,28 +2176,29 @@ final internal class _HashableTypedNativeDictionaryStorage<Key: Hashable, Value>
21762176
_ objects: UnsafeMutablePointer<AnyObject>?,
21772177
andKeys keys: UnsafeMutablePointer<AnyObject>?,
21782178
count: Int) {
2179+
_precondition(count >= 0, "Invalid count")
21792180
// The user is expected to provide a storage of the correct size
21802181
if let unmanagedKeys = _UnmanagedAnyObjectArray(keys) {
21812182
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21822183
// keys nonnull, objects nonnull
21832184
for (offset: i, element: (key: key, value: val)) in full.enumerated() {
2185+
guard i < count else { break }
21842186
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
21852187
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
2186-
guard i < count else { break }
21872188
}
21882189
} else {
21892190
// keys nonnull, objects null
21902191
for (offset: i, element: (key: key, value: _)) in full.enumerated() {
2191-
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
21922192
guard i < count else { break }
2193+
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
21932194
}
21942195
}
21952196
} else {
21962197
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21972198
// keys null, objects nonnull
21982199
for (offset: i, element: (key: _, value: val)) in full.enumerated() {
2199-
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
22002200
guard i < count else { break }
2201+
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
22012202
}
22022203
} else {
22032204
// do nothing, both are null
@@ -2969,6 +2970,7 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29692970
_ keys: UnsafeMutablePointer<AnyObject>?,
29702971
_ count: Int
29712972
) {
2973+
_precondition(count >= 0, "Invalid count")
29722974
bridgeEverything()
29732975
// The user is expected to provide a storage of the correct size
29742976
var i = 0 // Position in the input storage
@@ -2979,19 +2981,19 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29792981
// keys nonnull, objects nonnull
29802982
for position in 0..<bucketCount {
29812983
if bridgedBuffer.isInitializedEntry(at: position) {
2984+
guard i < count else { break }
29822985
unmanagedObjects[i] = bridgedBuffer.value(at: position)
29832986
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29842987
i += 1
2985-
guard i < count else { break }
29862988
}
29872989
}
29882990
} else {
29892991
// keys nonnull, objects null
29902992
for position in 0..<bucketCount {
29912993
if bridgedBuffer.isInitializedEntry(at: position) {
2994+
guard i < count else { break }
29922995
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29932996
i += 1
2994-
guard i < count else { break }
29952997
}
29962998
}
29972999
}
@@ -3000,9 +3002,9 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
30003002
// keys null, objects nonnull
30013003
for position in 0..<bucketCount {
30023004
if bridgedBuffer.isInitializedEntry(at: position) {
3005+
guard i < count else { break }
30033006
unmanagedObjects[i] = bridgedBuffer.value(at: position)
30043007
i += 1
3005-
guard i < count else { break }
30063008
}
30073009
}
30083010
} else {

0 commit comments

Comments
 (0)