Skip to content

Commit

Permalink
Fix crash when creating 'KeychainSettings' without name on mac.
Browse files Browse the repository at this point in the history
  • Loading branch information
MJegorovas committed Nov 15, 2023
1 parent 4df8b17 commit 3c820c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ public class KeychainSettings @ExperimentalSettingsApi constructor(vararg defaul
}

@Suppress("RemoveRedundantCallsOfConversionMethods") // IDE thinks CFIndex == Int but might be Long
val list = List(CFArrayGetCount(attributes.value).toInt()) { i ->
val size = CFArrayGetCount(attributes.value).toInt()
return (0 until size).mapNotNullTo(mutableSetOf()) { i ->
val item: CFDictionaryRef? = CFArrayGetValueAtIndex(attributes.value, i.toCFIndex())?.reinterpret()
val cfKey: CFStringRef? = CFDictionaryGetValue(item, kSecAttrAccount)?.reinterpret()
val nsKey = CFBridgingRelease(cfKey) as NSString
nsKey.toKString()
if (cfKey != null) {
val nsKey = CFBridgingRelease(cfKey) as NSString
nsKey.toKString()
} else null
}
return list.toSet()
}

public override val size: Int get() = keys.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import platform.Security.kSecMatchLimitOne
import platform.Security.kSecReturnData
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

// TODO figure out how to get this running on ios, watchos, and tvos simulators
@ExperimentalSettingsImplementation
Expand Down Expand Up @@ -73,4 +74,10 @@ class KeychainSettingsTest : BaseSettingsTest(
}
assertEquals("value", value)
}

@Test
fun keys_no_name() {
val settings = KeychainSettings()
assertTrue(settings.keys.isNotEmpty())
}
}

0 comments on commit 3c820c1

Please sign in to comment.