Merge "Add MotionEvent information to exception" into tm-dev

This commit is contained in:
TreeHugger Robot
2022-06-03 21:21:05 +00:00
committed by Android (Google) Code Review

View File

@@ -16,7 +16,7 @@
#define LOG_TAG "MotionEvent-JNI" #define LOG_TAG "MotionEvent-JNI"
#include <nativehelper/JNIHelp.h> #include "android_view_MotionEvent.h"
#include <android/graphics/matrix.h> #include <android/graphics/matrix.h>
#include <android_runtime/AndroidRuntime.h> #include <android_runtime/AndroidRuntime.h>
@@ -24,12 +24,12 @@
#include <attestation/HmacKeyManager.h> #include <attestation/HmacKeyManager.h>
#include <gui/constants.h> #include <gui/constants.h>
#include <input/Input.h> #include <input/Input.h>
#include <log/log.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h> #include <nativehelper/ScopedUtfChars.h>
#include <utils/Log.h>
#include "android_os_Parcel.h" #include "android_os_Parcel.h"
#include "android_util_Binder.h" #include "android_util_Binder.h"
#include "android_view_MotionEvent.h"
#include "core_jni_helpers.h" #include "core_jni_helpers.h"
namespace android { namespace android {
@@ -158,19 +158,21 @@ static bool validatePointerCoordsObjArray(JNIEnv* env, jobjectArray pointerCoord
return true; return true;
} }
static bool validatePointerIndex(JNIEnv* env, jint pointerIndex, size_t pointerCount) { static bool validatePointerIndex(JNIEnv* env, jint pointerIndex, const MotionEvent& event) {
if (pointerIndex < 0 || size_t(pointerIndex) >= pointerCount) { if (pointerIndex < 0 || size_t(pointerIndex) >= event.getPointerCount()) {
jniThrowException(env, "java/lang/IllegalArgumentException", std::stringstream message;
"pointerIndex out of range"); message << "invalid pointerIndex " << pointerIndex << " for " << event;
jniThrowException(env, "java/lang/IllegalArgumentException", message.str().c_str());
return false; return false;
} }
return true; return true;
} }
static bool validateHistoryPos(JNIEnv* env, jint historyPos, size_t historySize) { static bool validateHistoryPos(JNIEnv* env, jint historyPos, const MotionEvent& event) {
if (historyPos < 0 || size_t(historyPos) >= historySize) { if (historyPos < 0 || size_t(historyPos) >= event.getHistorySize()) {
jniThrowException(env, "java/lang/IllegalArgumentException", std::stringstream message;
"historyPos out of range"); message << "historyPos " << historyPos << " out of range for " << event;
jniThrowException(env, "java/lang/IllegalArgumentException", message.str().c_str());
return false; return false;
} }
return true; return true;
@@ -399,13 +401,11 @@ static void android_view_MotionEvent_nativeAddBatch(JNIEnv* env, jclass clazz,
static void android_view_MotionEvent_nativeGetPointerCoords(JNIEnv* env, jclass clazz, static void android_view_MotionEvent_nativeGetPointerCoords(JNIEnv* env, jclass clazz,
jlong nativePtr, jint pointerIndex, jint historyPos, jobject outPointerCoordsObj) { jlong nativePtr, jint pointerIndex, jint historyPos, jobject outPointerCoordsObj) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount(); if (!validatePointerIndex(env, pointerIndex, *event) ||
if (!validatePointerIndex(env, pointerIndex, pointerCount) !validatePointerCoords(env, outPointerCoordsObj)) {
|| !validatePointerCoords(env, outPointerCoordsObj)) {
return; return;
} }
if (historyPos != HISTORY_CURRENT && if (historyPos != HISTORY_CURRENT && !validateHistoryPos(env, historyPos, *event)) {
!validateHistoryPos(env, historyPos, event->getHistorySize())) {
return; return;
} }
@@ -445,9 +445,8 @@ static void android_view_MotionEvent_nativeGetPointerCoords(JNIEnv* env, jclass
static void android_view_MotionEvent_nativeGetPointerProperties(JNIEnv* env, jclass clazz, static void android_view_MotionEvent_nativeGetPointerProperties(JNIEnv* env, jclass clazz,
jlong nativePtr, jint pointerIndex, jobject outPointerPropertiesObj) { jlong nativePtr, jint pointerIndex, jobject outPointerPropertiesObj) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount(); if (!validatePointerIndex(env, pointerIndex, *event) ||
if (!validatePointerIndex(env, pointerIndex, pointerCount) !validatePointerProperties(env, outPointerPropertiesObj)) {
|| !validatePointerProperties(env, outPointerPropertiesObj)) {
return; return;
} }
@@ -502,8 +501,7 @@ static jint android_view_MotionEvent_nativeAxisFromString(JNIEnv* env, jclass cl
static jint android_view_MotionEvent_nativeGetPointerId(JNIEnv* env, jclass clazz, static jint android_view_MotionEvent_nativeGetPointerId(JNIEnv* env, jclass clazz,
jlong nativePtr, jint pointerIndex) { jlong nativePtr, jint pointerIndex) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount(); if (!validatePointerIndex(env, pointerIndex, *event)) {
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return -1; return -1;
} }
return event->getPointerId(pointerIndex); return event->getPointerId(pointerIndex);
@@ -512,8 +510,7 @@ static jint android_view_MotionEvent_nativeGetPointerId(JNIEnv* env, jclass claz
static jint android_view_MotionEvent_nativeGetToolType(JNIEnv* env, jclass clazz, static jint android_view_MotionEvent_nativeGetToolType(JNIEnv* env, jclass clazz,
jlong nativePtr, jint pointerIndex) { jlong nativePtr, jint pointerIndex) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount(); if (!validatePointerIndex(env, pointerIndex, *event)) {
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return -1; return -1;
} }
return event->getToolType(pointerIndex); return event->getToolType(pointerIndex);
@@ -525,8 +522,7 @@ static jlong android_view_MotionEvent_nativeGetEventTimeNanos(JNIEnv* env, jclas
if (historyPos == HISTORY_CURRENT) { if (historyPos == HISTORY_CURRENT) {
return event->getEventTime(); return event->getEventTime();
} else { } else {
size_t historySize = event->getHistorySize(); if (!validateHistoryPos(env, historyPos, *event)) {
if (!validateHistoryPos(env, historyPos, historySize)) {
return 0; return 0;
} }
return event->getHistoricalEventTime(historyPos); return event->getHistoricalEventTime(historyPos);
@@ -537,16 +533,14 @@ static jfloat android_view_MotionEvent_nativeGetRawAxisValue(JNIEnv* env, jclass
jlong nativePtr, jint axis, jlong nativePtr, jint axis,
jint pointerIndex, jint historyPos) { jint pointerIndex, jint historyPos) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount(); if (!validatePointerIndex(env, pointerIndex, *event)) {
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return 0; return 0;
} }
if (historyPos == HISTORY_CURRENT) { if (historyPos == HISTORY_CURRENT) {
return event->getRawAxisValue(axis, pointerIndex); return event->getRawAxisValue(axis, pointerIndex);
} else { } else {
size_t historySize = event->getHistorySize(); if (!validateHistoryPos(env, historyPos, *event)) {
if (!validateHistoryPos(env, historyPos, historySize)) {
return 0; return 0;
} }
return event->getHistoricalRawAxisValue(axis, pointerIndex, historyPos); return event->getHistoricalRawAxisValue(axis, pointerIndex, historyPos);
@@ -556,16 +550,14 @@ static jfloat android_view_MotionEvent_nativeGetRawAxisValue(JNIEnv* env, jclass
static jfloat android_view_MotionEvent_nativeGetAxisValue(JNIEnv* env, jclass clazz, static jfloat android_view_MotionEvent_nativeGetAxisValue(JNIEnv* env, jclass clazz,
jlong nativePtr, jint axis, jint pointerIndex, jint historyPos) { jlong nativePtr, jint axis, jint pointerIndex, jint historyPos) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr); MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount(); if (!validatePointerIndex(env, pointerIndex, *event)) {
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return 0; return 0;
} }
if (historyPos == HISTORY_CURRENT) { if (historyPos == HISTORY_CURRENT) {
return event->getAxisValue(axis, pointerIndex); return event->getAxisValue(axis, pointerIndex);
} else { } else {
size_t historySize = event->getHistorySize(); if (!validateHistoryPos(env, historyPos, (*event))) {
if (!validateHistoryPos(env, historyPos, historySize)) {
return 0; return 0;
} }
return event->getHistoricalAxisValue(axis, pointerIndex, historyPos); return event->getHistoricalAxisValue(axis, pointerIndex, historyPos);