OSDN Git Service

Fix wrong array index bound in NotificationUsageStats
authorKouji Shiotani <kouji.x.shiotani@sonymobile.com>
Thu, 16 Mar 2017 06:20:49 +0000 (15:20 +0900)
committerShunta Sato <shunta.sato@sonymobile.com>
Fri, 31 Mar 2017 12:40:16 +0000 (21:40 +0900)
Symptom:
It's possible to cause ArrayIndexOutOfBoundsException.

Root cause:
There is an error in the argument check, there is a possibility of
accessing an incorrect Index

Solution:
Fix the argument check correctly

Bug: 36542230

Change-Id: I3dea387c4a2bbddcc68db06812b57fd9d300f778

services/core/java/com/android/server/notification/NotificationUsageStats.java

index 34c5283..4265741 100644 (file)
@@ -40,6 +40,7 @@ import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.io.PrintWriter;
+import java.lang.Math;
 import java.util.ArrayDeque;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
@@ -718,8 +719,8 @@ public class NotificationUsageStats {
         }
 
         void increment(int imp) {
-            imp = imp < 0 ? 0 : imp > NUM_IMPORTANCES ? NUM_IMPORTANCES : imp;
-            mCount[imp] ++;
+            imp = Math.max(0, Math.min(imp, mCount.length - 1));
+            mCount[imp]++;
         }
 
         void maybeCount(ImportanceHistogram prev) {