From 0dc291eef630cbc3c4479fff8c549567ac980f87 Mon Sep 17 00:00:00 2001 From: Johan Rosengren Date: Mon, 21 Feb 2011 09:49:45 +0100 Subject: [PATCH] Protecting more views from (bad) MotionEvents When handling MotionEvents, the method findPointerIndex can return -1 if the current pointer id can't be translated to a pointer index. Some views are not handling this, which will lead to an out-of- index crash. In 2585e9b there were checks added, this change adds some more. Change-Id: I93ce2420afd83a06b689a1ed35ead7d170cd68f1 --- core/java/android/widget/HorizontalScrollView.java | 13 +++++++++++++ core/java/android/widget/ScrollView.java | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java index 18c4fe631af3a..ff0579cd78f9f 100644 --- a/core/java/android/widget/HorizontalScrollView.java +++ b/core/java/android/widget/HorizontalScrollView.java @@ -22,6 +22,7 @@ import android.graphics.Canvas; import android.graphics.Rect; import android.os.Bundle; import android.util.AttributeSet; +import android.util.Log; import android.view.FocusFinder; import android.view.InputDevice; import android.view.KeyEvent; @@ -62,6 +63,7 @@ public class HorizontalScrollView extends FrameLayout { private static final float MAX_SCROLL_FACTOR = ScrollView.MAX_SCROLL_FACTOR; + private static final String TAG = "HorizontalScrollView"; private long mLastScroll; @@ -456,6 +458,12 @@ public class HorizontalScrollView extends FrameLayout { } final int pointerIndex = ev.findPointerIndex(activePointerId); + if (pointerIndex == -1) { + Log.e(TAG, "Invalid pointerId=" + activePointerId + + " in onInterceptTouchEvent"); + break; + } + final int x = (int) ev.getX(pointerIndex); final int xDiff = (int) Math.abs(x - mLastMotionX); if (xDiff > mTouchSlop) { @@ -557,6 +565,11 @@ public class HorizontalScrollView extends FrameLayout { } case MotionEvent.ACTION_MOVE: final int activePointerIndex = ev.findPointerIndex(mActivePointerId); + if (activePointerIndex == -1) { + Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent"); + break; + } + final int x = (int) ev.getX(activePointerIndex); int deltaX = mLastMotionX - x; if (!mIsBeingDragged && Math.abs(deltaX) > mTouchSlop) { diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java index ebc54f4527c81..8747dc3dba5f1 100644 --- a/core/java/android/widget/ScrollView.java +++ b/core/java/android/widget/ScrollView.java @@ -25,6 +25,7 @@ import android.graphics.Rect; import android.os.Bundle; import android.os.StrictMode; import android.util.AttributeSet; +import android.util.Log; import android.view.FocusFinder; import android.view.InputDevice; import android.view.KeyEvent; @@ -69,6 +70,8 @@ public class ScrollView extends FrameLayout { static final float MAX_SCROLL_FACTOR = 0.5f; + private static final String TAG = "ScrollView"; + private long mLastScroll; private final Rect mTempRect = new Rect(); @@ -478,6 +481,12 @@ public class ScrollView extends FrameLayout { } final int pointerIndex = ev.findPointerIndex(activePointerId); + if (pointerIndex == -1) { + Log.e(TAG, "Invalid pointerId=" + activePointerId + + " in onInterceptTouchEvent"); + break; + } + final int y = (int) ev.getY(pointerIndex); final int yDiff = Math.abs(y - mLastMotionY); if (yDiff > mTouchSlop) { @@ -585,6 +594,11 @@ public class ScrollView extends FrameLayout { } case MotionEvent.ACTION_MOVE: final int activePointerIndex = ev.findPointerIndex(mActivePointerId); + if (activePointerIndex == -1) { + Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent"); + break; + } + final int y = (int) ev.getY(activePointerIndex); int deltaY = mLastMotionY - y; if (!mIsBeingDragged && Math.abs(deltaY) > mTouchSlop) {