From 3a0b122b8d7d7c334f1180f04a759bb5c61e568f Mon Sep 17 00:00:00 2001 From: Nader Jawad Date: Thu, 1 Apr 2021 20:34:57 -0700 Subject: [PATCH] Fixed NaN exceptions in EdgeEffect Added more error checking logic to protect against invalid usages of EdgeEffect that include setSize not being called or invalid bounds parameters. Fixes: 183945458 Fixes: 183895581 Fixes: 183781985 Test: Added CTS test to EdgeEffectTests Change-Id: I1eabd96206216526dbe843d0fb8c0fe6cccd8395 --- core/java/android/widget/EdgeEffect.java | 35 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/core/java/android/widget/EdgeEffect.java b/core/java/android/widget/EdgeEffect.java index 34ad659d148c1..efc023900a632 100644 --- a/core/java/android/widget/EdgeEffect.java +++ b/core/java/android/widget/EdgeEffect.java @@ -626,20 +626,31 @@ public class EdgeEffect { // assume rotations of increments of 90 degrees float x = mTmpPoints[10] - mTmpPoints[8]; float width = right - left; - float vecX = dampStretchVector(Math.max(-1f, Math.min(1f, x / width))); + float vecX = 0f; + if (width > 0) { + vecX = dampStretchVector(Math.max(-1f, Math.min(1f, x / width))); + } + float y = mTmpPoints[11] - mTmpPoints[9]; float height = bottom - top; - float vecY = dampStretchVector(Math.max(-1f, Math.min(1f, y / height))); - renderNode.stretch( - left, - top, - right, - bottom, - vecX, - vecY, - mWidth, - mHeight - ); + float vecY = 0f; + if (height > 0) { + vecY = dampStretchVector(Math.max(-1f, Math.min(1f, y / height))); + } + + boolean hasStretchVectors = Float.compare(vecX, 0) != 0 || Float.compare(vecY, 0) != 0; + if (right > left && bottom > top && mWidth > 0 && mHeight > 0 && hasStretchVectors) { + renderNode.stretch( + left, + top, + right, + bottom, + vecX, + vecY, + mWidth, + mHeight + ); + } } else { // This is TYPE_STRETCH and drawing into a Canvas that isn't a Recording Canvas, // so no effect can be shown. Just end the effect.