From b192344234dcee215462e6218cf1c4384b9916f1 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Thu, 2 Mar 2017 19:26:44 +0000 Subject: [PATCH] MathUtils: Remove static Random field. This is unsafe because the random will be seeded in the zygote, which means all processes on the system generate the same sequence of random numbers. Fortunately, this code is only used in tests, so get rid of it. Note that the Random that backs Math.random() is reseeded after every fork to avoid a similar issue. Bug: 35918685 Test: HwAccelerationTest Change-Id: Ice79aa25bb9017f7a0b91659afe04112850cb74b --- core/java/android/util/MathUtils.java | 23 ------------------- .../test/hwui/PathDestructionActivity.java | 18 +++++++++------ 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/core/java/android/util/MathUtils.java b/core/java/android/util/MathUtils.java index 32aac13d39b49..5c718f1a9a1ec 100644 --- a/core/java/android/util/MathUtils.java +++ b/core/java/android/util/MathUtils.java @@ -24,7 +24,6 @@ import java.util.Random; * @hide Pending API council approval */ public final class MathUtils { - private static final Random sRandom = new Random(); private static final float DEG_TO_RAD = 3.1415926f / 180.0f; private static final float RAD_TO_DEG = 180.0f / 3.1415926f; @@ -185,28 +184,6 @@ public final class MathUtils { return maxStart + (maxStart - maxStop) * ((value - minStart) / (minStop - minStart)); } - public static int random(int howbig) { - return (int) (sRandom.nextFloat() * howbig); - } - - public static int random(int howsmall, int howbig) { - if (howsmall >= howbig) return howsmall; - return (int) (sRandom.nextFloat() * (howbig - howsmall) + howsmall); - } - - public static float random(float howbig) { - return sRandom.nextFloat() * howbig; - } - - public static float random(float howsmall, float howbig) { - if (howsmall >= howbig) return howsmall; - return sRandom.nextFloat() * (howbig - howsmall) + howsmall; - } - - public static void randomSeed(long seed) { - sRandom.setSeed(seed); - } - /** * Returns the sum of the two parameters, or throws an exception if the resulting sum would * cause an overflow or underflow. diff --git a/tests/HwAccelerationTest/src/com/android/test/hwui/PathDestructionActivity.java b/tests/HwAccelerationTest/src/com/android/test/hwui/PathDestructionActivity.java index 4177725e38470..5cede6540410b 100644 --- a/tests/HwAccelerationTest/src/com/android/test/hwui/PathDestructionActivity.java +++ b/tests/HwAccelerationTest/src/com/android/test/hwui/PathDestructionActivity.java @@ -25,6 +25,8 @@ import android.os.Bundle; import android.util.MathUtils; import android.view.View; +import java.util.Random; + /** * The point of this test is to ensure that we can cause many paths to be created, drawn, * and destroyed without causing hangs or crashes. This tests the native reference counting @@ -57,10 +59,11 @@ public class PathDestructionActivity extends Activity { private Path getRandomPath() { float left, top, right, bottom; - left = MathUtils.random(getWidth() - MIN_SIZE); - top = MathUtils.random(getHeight() - MIN_SIZE); - right = left + MathUtils.random(getWidth() - left); - bottom = top + MathUtils.random(getHeight() - top); + Random r = new Random(); + left = r.nextFloat() * (getWidth() - MIN_SIZE); + top = r.nextFloat() * (getHeight() - MIN_SIZE); + right = left + r.nextFloat() * (getWidth() - left); + bottom = top + r.nextFloat() * (getHeight() - top); Path path = new Path(); path.moveTo(left, top); path.lineTo(right, top); @@ -71,9 +74,10 @@ public class PathDestructionActivity extends Activity { } private int getRandomColor() { - int red = MathUtils.random(255); - int green = MathUtils.random(255); - int blue = MathUtils.random(255); + Random r = new Random(); + int red = r.nextInt(255); + int green = r.nextInt(255); + int blue = r.nextInt(255); return 0xff000000 | red << 16 | green << 8 | blue; }