From 57eb705e9b0c3c6c9387d4414157f63eb47e1c1f Mon Sep 17 00:00:00 2001 From: Rubin Xu Date: Fri, 14 Dec 2018 11:10:07 +0000 Subject: [PATCH] Fix iterator misuse Fix a bug where we iterate through a map with an iterator while deleting some elements (on the map directly, not using the iterator) at the same time. Bug: 112982695 Test: atest com.android.cts.devicepolicy.MixedProfileOwnerTest#testDelegation Change-Id: Id46701791d9666ed653683f41e82e3b1d2288432 --- .../server/devicepolicy/DevicePolicyManagerService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java index 7186cdf96dee2..f7c2b09914fa5 100644 --- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java +++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java @@ -5925,9 +5925,9 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { // If set, remove exclusive scopes from all other delegates if (exclusiveScopes != null && !exclusiveScopes.isEmpty()) { - for (Map.Entry> entry : policy.mDelegationMap.entrySet()) { - final String currentPackage = entry.getKey(); - final List currentScopes = entry.getValue(); + for (int i = policy.mDelegationMap.size() - 1; i >= 0; --i) { + final String currentPackage = policy.mDelegationMap.keyAt(i); + final List currentScopes = policy.mDelegationMap.valueAt(i); if (!currentPackage.equals(delegatePackage)) { // Iterate through all other delegates @@ -5935,7 +5935,7 @@ public class DevicePolicyManagerService extends BaseIDevicePolicyManager { // And if this delegate had some exclusive scopes which are now moved // to the new delegate, notify about its delegation changes. if (currentScopes.isEmpty()) { - policy.mDelegationMap.remove(currentPackage); + policy.mDelegationMap.removeAt(i); } sendDelegationChangedBroadcast(currentPackage, new ArrayList<>(currentScopes), userId);