Skip to content

Commit 59cad33

Browse files
errorcodeQQerrorcodeQQ
authored andcommitted
fix: GuardedContext policy lambda evaluation on initialization bug
1 parent 3f5c93b commit 59cad33

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

CSGuard/src/main/kotlin/com/csguard/CSGuardPlugin.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,10 @@ class ActivityContextHook(
181181
try {
182182
val current = field.get(activity) as? Context ?: return
183183
if (current is GuardedContext) return // already wrapped
184-
val wrapped = GuardedContext(current, policyProvider())
184+
val wrapped = GuardedContext(current, policyProvider)
185185
field.set(activity, wrapped)
186186
Log.i(TAG, "✓ Wrapped Activity mBase: ${activity.javaClass.simpleName}")
187187
} catch (t: Throwable) {
188-
189-
}
188+
}
190189
}
191190
}

CSGuard/src/main/kotlin/com/csguard/GuardedContext.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import android.content.ContextWrapper
1212

1313
class GuardedContext(
1414
base: Context,
15-
private val policy: GuardPolicy = GuardPolicy.DEFAULT,
15+
private val policyProvider: () -> GuardPolicy,
1616
private val providerName: String? = null
1717
) : ContextWrapper(base) {
1818

19+
private val policy: GuardPolicy
20+
get() = policyProvider()
21+
1922
companion object {
2023
private const val TAG = "CSGuard"
2124
}

CSGuard/src/main/kotlin/com/csguard/ProviderSanitizer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ wrappedTracker[provider] = true
130130
val current = field.get(provider) ?: continue
131131
if (current is GuardedContext) continue // already wrapped
132132
if (current !is Context) continue
133-
val wrapped = GuardedContext(current, policy, provider.name)
133+
val wrapped = GuardedContext(current, { policy }, provider.name)
134134
field.set(provider, wrapped)
135135
anyWrapped = true
136136
} catch (t: Throwable) {

0 commit comments

Comments
 (0)