Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8beafe3a authored by Yasin Kilicdere's avatar Yasin Kilicdere
Browse files

Fixes collection modified while iterating by cloning the collection.

During a user switch, in MediaRouterService, a collection of users was
modified while iterating, which ends up skipping some elements of the
collection unintentionally. This CL creates a clone of the collection
before the for loop to prevent the issue.

Bug: 260410380
Test: n/a
Change-Id: Iec4d460ba0a0ca1325506e729f58715bb2d68898
parent 2ff1284e
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -645,9 +645,11 @@ class MediaRouter2ServiceImpl {
                                "userId: %d", newActiveUserId));

                mCurrentActiveUserId = newActiveUserId;
                for (int i = 0; i < mUserRecords.size(); i++) {
                    int userId = mUserRecords.keyAt(i);
                    UserRecord userRecord = mUserRecords.valueAt(i);
                // disposeUserIfNeededLocked might modify the collection, hence clone
                final var userRecords = mUserRecords.clone();
                for (int i = 0; i < userRecords.size(); i++) {
                    int userId = userRecords.keyAt(i);
                    UserRecord userRecord = userRecords.valueAt(i);
                    if (isUserActiveLocked(userId)) {
                        // userId corresponds to the active user, or one of its profiles. We
                        // ensure the associated structures are initialized.
+5 −3
Original line number Diff line number Diff line
@@ -665,9 +665,11 @@ public final class MediaRouterService extends IMediaRouterService.Stub
        synchronized (mLock) {
            if (mCurrentActiveUserId != newActiveUserId) {
                mCurrentActiveUserId = newActiveUserId;
                for (int i = 0; i < mUserRecords.size(); i++) {
                    int userId = mUserRecords.keyAt(i);
                    UserRecord userRecord = mUserRecords.valueAt(i);
                // disposeUserIfNeededLocked might modify the collection, hence clone
                final var userRecords = mUserRecords.clone();
                for (int i = 0; i < userRecords.size(); i++) {
                    int userId = userRecords.keyAt(i);
                    UserRecord userRecord = userRecords.valueAt(i);
                    if (isUserActiveLocked(userId)) {
                        // userId corresponds to the active user, or one of its profiles. We
                        // ensure the associated structures are initialized.