From bbfe962fff3d654b1fa19c0876e86c6dc4d16df2 Mon Sep 17 00:00:00 2001 From: James O'Leary Date: Tue, 2 Mar 2021 19:52:26 -0500 Subject: [PATCH] Fix crash when number of pixels < max colors If extraction is run on a set of pixels whose count is less than the number of max colors, then colors.length < max colors, and accesses to colors[index] inside the for loop throw an ArrayOutOfBoundsException. Test: add widget to home screen, reduce size of widget to small area. observe crash. `mp core services` with fix, observe that crash no longer occurs. `mp core services` again without fix, verify crash occurs after boot. Fixes: 181130209 Change-Id: I60bf1ab39f7a69f2fb0745049fd46d2f0b53a12c --- .../com/android/internal/graphics/palette/WuQuantizer.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/java/com/android/internal/graphics/palette/WuQuantizer.java b/core/java/com/android/internal/graphics/palette/WuQuantizer.java index 01e45f6139864..66206bf8297a4 100644 --- a/core/java/com/android/internal/graphics/palette/WuQuantizer.java +++ b/core/java/com/android/internal/graphics/palette/WuQuantizer.java @@ -16,7 +16,6 @@ package com.android.internal.graphics.palette; - import java.util.ArrayList; import java.util.List; @@ -120,7 +119,11 @@ public class WuQuantizer implements Quantizer { } } - for (k = 0; k < mMaxColors; ++k) { + // If extraction is run on a set of pixels whose count is less than the + // number of max colors, then colors.length < max colors, and accesses + // to colors[index] inside the for loop throw an ArrayOutOfBoundsException. + int numColorsToCreate = (int) Math.min(mMaxColors, colors.length); + for (k = 0; k < numColorsToCreate; ++k) { weight = getVolume(cube[k], mWt); if (weight > 0) { red = (int) (getVolume(cube[k], mMr) / weight);