From 861bdbfb567c8718c5ea7b2170b89e8114033ad9 Mon Sep 17 00:00:00 2001 From: Alessio Balsini Date: Mon, 1 Jun 2020 13:27:34 +0100 Subject: [PATCH] KernelCpuUidBpfMapReader: Verify presence of Uids before deleting Make sure the first and last Uids are valid exist in the sparse array before removing the array range of which they are the boundaries. If they are valid, but not exist, create fake elements to make sure all the elemens in the range are deleted. Bug: 157887803 Test: build Signed-off-by: Alessio Balsini Change-Id: I3b5ddc499178163cb9b0e9cabbe4f3457ae34dad --- .../internal/os/KernelCpuUidBpfMapReader.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/java/com/android/internal/os/KernelCpuUidBpfMapReader.java b/core/java/com/android/internal/os/KernelCpuUidBpfMapReader.java index 26f81d9db9c8e..dafb924f37bd1 100644 --- a/core/java/com/android/internal/os/KernelCpuUidBpfMapReader.java +++ b/core/java/com/android/internal/os/KernelCpuUidBpfMapReader.java @@ -16,7 +16,6 @@ package com.android.internal.os; -import android.os.StrictMode; import android.os.SystemClock; import android.util.Slog; import android.util.SparseArray; @@ -90,9 +89,21 @@ public abstract class KernelCpuUidBpfMapReader { if (mErrors > ERROR_THRESHOLD) { return; } + if (endUid < startUid || startUid < 0) { + return; + } + mWriteLock.lock(); int firstIndex = mData.indexOfKey(startUid); + if (firstIndex < 0) { + mData.put(startUid, null); + firstIndex = mData.indexOfKey(startUid); + } int lastIndex = mData.indexOfKey(endUid); + if (lastIndex < 0) { + mData.put(endUid, null); + lastIndex = mData.indexOfKey(endUid); + } mData.removeAtRange(firstIndex, lastIndex - firstIndex + 1); mWriteLock.unlock(); }