Skip to content

Commit a951cf2

Browse files
committed
[stdlib] Dictionary: Fix getObjects:andKeys:count: implementations
1 parent 91bb2c0 commit a951cf2

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
@@ -2138,28 +2138,29 @@ final internal class _HashableTypedNativeDictionaryStorage<Key: Hashable, Value>
21382138
_ objects: UnsafeMutablePointer<AnyObject>?,
21392139
andKeys keys: UnsafeMutablePointer<AnyObject>?,
21402140
count: Int) {
2141+
_precondition(count >= 0, "Invalid count")
21412142
// The user is expected to provide a storage of the correct size
21422143
if let unmanagedKeys = _UnmanagedAnyObjectArray(keys) {
21432144
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21442145
// keys nonnull, objects nonnull
21452146
for (offset: i, element: (key: key, value: val)) in full.enumerated() {
2147+
guard i < count else { break }
21462148
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
21472149
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
2148-
guard i < count else { break }
21492150
}
21502151
} else {
21512152
// keys nonnull, objects null
21522153
for (offset: i, element: (key: key, value: _)) in full.enumerated() {
2153-
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
21542154
guard i < count else { break }
2155+
unmanagedKeys[i] = _bridgeAnythingToObjectiveC(key)
21552156
}
21562157
}
21572158
} else {
21582159
if let unmanagedObjects = _UnmanagedAnyObjectArray(objects) {
21592160
// keys null, objects nonnull
21602161
for (offset: i, element: (key: _, value: val)) in full.enumerated() {
2161-
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
21622162
guard i < count else { break }
2163+
unmanagedObjects[i] = _bridgeAnythingToObjectiveC(val)
21632164
}
21642165
} else {
21652166
// do nothing, both are null
@@ -2942,6 +2943,7 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29422943
_ keys: UnsafeMutablePointer<AnyObject>?,
29432944
_ count: Int
29442945
) {
2946+
_precondition(count >= 0, "Invalid count")
29452947
bridgeEverything()
29462948
// The user is expected to provide a storage of the correct size
29472949
var i = 0 // Position in the input storage
@@ -2952,19 +2954,19 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29522954
// keys nonnull, objects nonnull
29532955
for position in 0..<bucketCount {
29542956
if bridgedBuffer.isInitializedEntry(at: position) {
2957+
guard i < count else { break }
29552958
unmanagedObjects[i] = bridgedBuffer.value(at: position)
29562959
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29572960
i += 1
2958-
guard i < count else { break }
29592961
}
29602962
}
29612963
} else {
29622964
// keys nonnull, objects null
29632965
for position in 0..<bucketCount {
29642966
if bridgedBuffer.isInitializedEntry(at: position) {
2967+
guard i < count else { break }
29652968
unmanagedKeys[i] = bridgedBuffer.key(at: position)
29662969
i += 1
2967-
guard i < count else { break }
29682970
}
29692971
}
29702972
}
@@ -2973,9 +2975,9 @@ final internal class _SwiftDeferredNSDictionary<Key: Hashable, Value>
29732975
// keys null, objects nonnull
29742976
for position in 0..<bucketCount {
29752977
if bridgedBuffer.isInitializedEntry(at: position) {
2978+
guard i < count else { break }
29762979
unmanagedObjects[i] = bridgedBuffer.value(at: position)
29772980
i += 1
2978-
guard i < count else { break }
29792981
}
29802982
}
29812983
} else {

0 commit comments

Comments
 (0)