From e13adb925d748cb80f317f5305abe0e4468e1629 Mon Sep 17 00:00:00 2001 From: JingMatrix Date: Sat, 31 Jan 2026 12:51:09 +0100 Subject: [PATCH] Correct misunderstanding of takeIf execution order MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous code incorrectly assumed `takeIf` prevents the execution of the receiver statement. Since `takeIf` is an extension function, the receiver—`InterceptorUtils.getTransactCode`—was evaluated eagerly *before* the version check predicate could run. This commit replaces the `takeIf` chain with a standard `if/else` block to ensure the reflection call is only executed when the API level supports it. Additionally, repeated `IKeystoreService.Stub::class.java` references were refactored into a `stubBinderClass` property. --- .../keystore/Keystore2Interceptor.kt | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt index ef524ce..d4a815c 100644 --- a/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt +++ b/app/src/main/java/org/matrix/TEESimulator/interception/keystore/Keystore2Interceptor.kt @@ -27,23 +27,24 @@ import org.matrix.TEESimulator.pki.CertificateHelper */ @SuppressLint("BlockedPrivateApi") object Keystore2Interceptor : AbstractKeystoreInterceptor() { + private val stubBinderClass = IKeystoreService.Stub::class.java + // Transaction codes for the IKeystoreService interface methods we are interested in. private val GET_KEY_ENTRY_TRANSACTION = - InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "getKeyEntry") + InterceptorUtils.getTransactCode(stubBinderClass, "getKeyEntry") private val DELETE_KEY_TRANSACTION = - InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "deleteKey") + InterceptorUtils.getTransactCode(stubBinderClass, "deleteKey") private val UPDATE_SUBCOMPONENT_TRANSACTION = - InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "updateSubcomponent") + InterceptorUtils.getTransactCode(stubBinderClass, "updateSubcomponent") private val LIST_ENTRIES_TRANSACTION = - InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "listEntries") + InterceptorUtils.getTransactCode(stubBinderClass, "listEntries") private val LIST_ENTRIES_BATCHED_TRANSACTION = - InterceptorUtils.getTransactCode(IKeystoreService.Stub::class.java, "listEntriesBatched") - .takeIf { Build.VERSION.SDK_INT >= 34 } + if (Build.VERSION.SDK_INT >= 34) + InterceptorUtils.getTransactCode(stubBinderClass, "listEntriesBatched") + else null private val transactionNames: Map by lazy { - IKeystoreService.Stub::class - .java - .declaredFields + stubBinderClass.declaredFields .filter { it.isAccessible = true it.type == Int::class.java && it.name.startsWith("TRANSACTION_")