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

Commit 2c8c49c0 authored by lijilou's avatar lijilou Committed by Jilou li
Browse files

Usage:fix arrayIndexOutOfBoundsException to aviod

systemServer crash.

The object mPackagesTokenData should be accessed with mLock locked, except for the obfuscateCurrentStats and filterStats methods, which may cause multi-threaded access exceptions.

Test: OEM monkey test
Bug: 345626542
Change-Id: I8a89b68d407d6ff692f163bcb33e07c7dbc1a468
parent 36f8cfd9
Loading
Loading
Loading
Loading
+32 −25
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.util.SparseArray;
import android.util.TimeUtils;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.IndentingPrintWriter;

@@ -136,6 +137,7 @@ public class UsageStatsDatabase {
    // The obfuscated packages to tokens mappings file
    private final File mPackageMappingsFile;
    // Holds all of the data related to the obfuscated packages and their token mappings.
    @GuardedBy("mLock")
    final PackagesTokenData mPackagesTokenData = new PackagesTokenData();

    /**
@@ -771,6 +773,7 @@ public class UsageStatsDatabase {
     * all of the stats at once has an amortized cost for future calls.
     */
    void filterStats(IntervalStats stats) {
        synchronized (mLock) {
            if (mPackagesTokenData.removedPackagesMap.isEmpty()) {
                return;
            }
@@ -781,7 +784,8 @@ public class UsageStatsDatabase {
            for (int i = 0; i < removedPackagesSize; i++) {
                final String removedPackage = removedPackagesMap.keyAt(i);
                final UsageStats usageStats = stats.packageStats.get(removedPackage);
            if (usageStats != null && usageStats.mEndTimeStamp < removedPackagesMap.valueAt(i)) {
                if (usageStats != null &&
                        usageStats.mEndTimeStamp < removedPackagesMap.valueAt(i)) {
                    stats.packageStats.remove(removedPackage);
                }
            }
@@ -795,6 +799,7 @@ public class UsageStatsDatabase {
                }
            }
        }
    }

    /**
     * Figures out what to extract from the given IntervalStats object.
@@ -1226,6 +1231,7 @@ public class UsageStatsDatabase {
    }

    void obfuscateCurrentStats(IntervalStats[] currentStats) {
        synchronized (mLock) {
            if (mCurrentVersion < 5) {
                return;
            }
@@ -1234,6 +1240,7 @@ public class UsageStatsDatabase {
                stats.obfuscateData(mPackagesTokenData);
            }
        }
    }

    /**
     * Update the stats in the database. They may not be written to disk immediately.