Loading core/jni/com_android_internal_os_LongArrayMultiStateCounter.cpp +25 −21 Original line number Diff line number Diff line Loading @@ -99,11 +99,12 @@ static void throwWriteRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not write LongArrayMultiStateCounter to Parcel"); } #define THROW_ON_WRITE_ERROR(expr) \ #define THROW_AND_RETURN_ON_WRITE_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwWriteRE(env, status); \ return; \ } \ } Loading @@ -114,14 +115,15 @@ static void native_writeToParcel(JNIEnv *env, jobject self, jlong nativePtr, job ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); uint16_t stateCount = counter->getStateCount(); THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); // LongArrayMultiStateCounter has at least state 0 const std::vector<uint64_t> &anyState = counter->getCount(0); THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), anyState.size())); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), anyState.size())); for (battery::state_t state = 0; state < stateCount; state++) { THROW_ON_WRITE_ERROR(ndk::AParcel_writeVector(parcel.get(), counter->getCount(state))); THROW_AND_RETURN_ON_WRITE_ERROR( ndk::AParcel_writeVector(parcel.get(), counter->getCount(state))); } } Loading @@ -130,11 +132,12 @@ static void throwReadRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not read LongArrayMultiStateCounter from Parcel"); } #define THROW_ON_READ_ERROR(expr) \ #define THROW_AND_RETURN_ON_READ_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwReadRE(env, status); \ return 0L; \ } \ } Loading @@ -142,23 +145,24 @@ static jlong native_initFromParcel(JNIEnv *env, jclass theClass, jobject jParcel ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); int32_t stateCount; THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); int32_t arrayLength; THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &arrayLength)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &arrayLength)); battery::LongArrayMultiStateCounter *counter = new battery::LongArrayMultiStateCounter(stateCount, std::vector<uint64_t>(arrayLength)); auto counter = std::make_unique<battery::LongArrayMultiStateCounter>(stateCount, std::vector<uint64_t>( arrayLength)); std::vector<uint64_t> value; value.reserve(arrayLength); for (battery::state_t state = 0; state < stateCount; state++) { THROW_ON_READ_ERROR(ndk::AParcel_readVector(parcel.get(), &value)); THROW_AND_RETURN_ON_READ_ERROR(ndk::AParcel_readVector(parcel.get(), &value)); counter->setValue(state, value); } return reinterpret_cast<jlong>(counter); return reinterpret_cast<jlong>(counter.release()); } static jint native_getStateCount(jlong nativePtr) { Loading core/jni/com_android_internal_os_LongMultiStateCounter.cpp +20 −18 Original line number Diff line number Diff line Loading @@ -109,11 +109,12 @@ static void throwWriteRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not write LongMultiStateCounter to Parcel"); } #define THROW_ON_WRITE_ERROR(expr) \ #define THROW_AND_RETURN_ON_WRITE_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwWriteRE(env, status); \ return; \ } \ } Loading @@ -123,10 +124,10 @@ static void native_writeToParcel(JNIEnv *env, jobject self, jlong nativePtr, job ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); uint16_t stateCount = counter->getStateCount(); THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); for (battery::state_t state = 0; state < stateCount; state++) { THROW_ON_WRITE_ERROR(AParcel_writeInt64(parcel.get(), counter->getCount(state))); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt64(parcel.get(), counter->getCount(state))); } } Loading @@ -135,11 +136,12 @@ static void throwReadRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not read LongMultiStateCounter from Parcel"); } #define THROW_ON_READ_ERROR(expr) \ #define THROW_AND_RETURN_ON_READ_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwReadRE(env, status); \ return 0L; \ } \ } Loading @@ -147,17 +149,17 @@ static jlong native_initFromParcel(JNIEnv *env, jclass theClass, jobject jParcel ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); int32_t stateCount; THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); battery::LongMultiStateCounter *counter = new battery::LongMultiStateCounter(stateCount, 0); auto counter = std::make_unique<battery::LongMultiStateCounter>(stateCount, 0); for (battery::state_t state = 0; state < stateCount; state++) { int64_t value; THROW_ON_READ_ERROR(AParcel_readInt64(parcel.get(), &value)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt64(parcel.get(), &value)); counter->setValue(state, value); } return reinterpret_cast<jlong>(counter); return reinterpret_cast<jlong>(counter.release()); } static jint native_getStateCount(jlong nativePtr) { Loading core/tests/coretests/src/com/android/internal/os/LongArrayMultiStateCounterTest.java +12 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,8 @@ package com.android.internal.os; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.os.Parcel; import androidx.test.filters.SmallTest; Loading Loading @@ -149,4 +151,14 @@ public class LongArrayMultiStateCounterTest { container.getValues(counts); assertThat(counts).isEqualTo(expected); } @Test public void createFromBadParcel() { // Check we don't crash the runtime if the Parcel data is bad (b/243434675). Parcel parcel = Parcel.obtain(); parcel.writeInt(2); parcel.setDataPosition(0); assertThrows(RuntimeException.class, () -> LongArrayMultiStateCounter.CREATOR.createFromParcel(parcel)); } } core/tests/coretests/src/com/android/internal/os/LongMultiStateCounterTest.java +12 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,8 @@ package com.android.internal.os; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.os.Parcel; import androidx.test.filters.SmallTest; Loading Loading @@ -198,4 +200,14 @@ public class LongMultiStateCounterTest { assertThat(newCounter.getCount(0)).isEqualTo(116); } @Test public void createFromBadParcel() { // Check we don't crash the runtime if the Parcel data is bad (b/243434675). Parcel parcel = Parcel.obtain(); parcel.writeInt(13); parcel.setDataPosition(0); assertThrows(RuntimeException.class, () -> LongMultiStateCounter.CREATOR.createFromParcel(parcel)); } } Loading
core/jni/com_android_internal_os_LongArrayMultiStateCounter.cpp +25 −21 Original line number Diff line number Diff line Loading @@ -99,11 +99,12 @@ static void throwWriteRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not write LongArrayMultiStateCounter to Parcel"); } #define THROW_ON_WRITE_ERROR(expr) \ #define THROW_AND_RETURN_ON_WRITE_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwWriteRE(env, status); \ return; \ } \ } Loading @@ -114,14 +115,15 @@ static void native_writeToParcel(JNIEnv *env, jobject self, jlong nativePtr, job ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); uint16_t stateCount = counter->getStateCount(); THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); // LongArrayMultiStateCounter has at least state 0 const std::vector<uint64_t> &anyState = counter->getCount(0); THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), anyState.size())); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), anyState.size())); for (battery::state_t state = 0; state < stateCount; state++) { THROW_ON_WRITE_ERROR(ndk::AParcel_writeVector(parcel.get(), counter->getCount(state))); THROW_AND_RETURN_ON_WRITE_ERROR( ndk::AParcel_writeVector(parcel.get(), counter->getCount(state))); } } Loading @@ -130,11 +132,12 @@ static void throwReadRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not read LongArrayMultiStateCounter from Parcel"); } #define THROW_ON_READ_ERROR(expr) \ #define THROW_AND_RETURN_ON_READ_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwReadRE(env, status); \ return 0L; \ } \ } Loading @@ -142,23 +145,24 @@ static jlong native_initFromParcel(JNIEnv *env, jclass theClass, jobject jParcel ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); int32_t stateCount; THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); int32_t arrayLength; THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &arrayLength)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &arrayLength)); battery::LongArrayMultiStateCounter *counter = new battery::LongArrayMultiStateCounter(stateCount, std::vector<uint64_t>(arrayLength)); auto counter = std::make_unique<battery::LongArrayMultiStateCounter>(stateCount, std::vector<uint64_t>( arrayLength)); std::vector<uint64_t> value; value.reserve(arrayLength); for (battery::state_t state = 0; state < stateCount; state++) { THROW_ON_READ_ERROR(ndk::AParcel_readVector(parcel.get(), &value)); THROW_AND_RETURN_ON_READ_ERROR(ndk::AParcel_readVector(parcel.get(), &value)); counter->setValue(state, value); } return reinterpret_cast<jlong>(counter); return reinterpret_cast<jlong>(counter.release()); } static jint native_getStateCount(jlong nativePtr) { Loading
core/jni/com_android_internal_os_LongMultiStateCounter.cpp +20 −18 Original line number Diff line number Diff line Loading @@ -109,11 +109,12 @@ static void throwWriteRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not write LongMultiStateCounter to Parcel"); } #define THROW_ON_WRITE_ERROR(expr) \ #define THROW_AND_RETURN_ON_WRITE_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwWriteRE(env, status); \ return; \ } \ } Loading @@ -123,10 +124,10 @@ static void native_writeToParcel(JNIEnv *env, jobject self, jlong nativePtr, job ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); uint16_t stateCount = counter->getStateCount(); THROW_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt32(parcel.get(), stateCount)); for (battery::state_t state = 0; state < stateCount; state++) { THROW_ON_WRITE_ERROR(AParcel_writeInt64(parcel.get(), counter->getCount(state))); THROW_AND_RETURN_ON_WRITE_ERROR(AParcel_writeInt64(parcel.get(), counter->getCount(state))); } } Loading @@ -135,11 +136,12 @@ static void throwReadRE(JNIEnv *env, binder_status_t status) { jniThrowRuntimeException(env, "Could not read LongMultiStateCounter from Parcel"); } #define THROW_ON_READ_ERROR(expr) \ #define THROW_AND_RETURN_ON_READ_ERROR(expr) \ { \ binder_status_t status = expr; \ if (status != STATUS_OK) { \ throwReadRE(env, status); \ return 0L; \ } \ } Loading @@ -147,17 +149,17 @@ static jlong native_initFromParcel(JNIEnv *env, jclass theClass, jobject jParcel ndk::ScopedAParcel parcel(AParcel_fromJavaParcel(env, jParcel)); int32_t stateCount; THROW_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt32(parcel.get(), &stateCount)); battery::LongMultiStateCounter *counter = new battery::LongMultiStateCounter(stateCount, 0); auto counter = std::make_unique<battery::LongMultiStateCounter>(stateCount, 0); for (battery::state_t state = 0; state < stateCount; state++) { int64_t value; THROW_ON_READ_ERROR(AParcel_readInt64(parcel.get(), &value)); THROW_AND_RETURN_ON_READ_ERROR(AParcel_readInt64(parcel.get(), &value)); counter->setValue(state, value); } return reinterpret_cast<jlong>(counter); return reinterpret_cast<jlong>(counter.release()); } static jint native_getStateCount(jlong nativePtr) { Loading
core/tests/coretests/src/com/android/internal/os/LongArrayMultiStateCounterTest.java +12 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,8 @@ package com.android.internal.os; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.os.Parcel; import androidx.test.filters.SmallTest; Loading Loading @@ -149,4 +151,14 @@ public class LongArrayMultiStateCounterTest { container.getValues(counts); assertThat(counts).isEqualTo(expected); } @Test public void createFromBadParcel() { // Check we don't crash the runtime if the Parcel data is bad (b/243434675). Parcel parcel = Parcel.obtain(); parcel.writeInt(2); parcel.setDataPosition(0); assertThrows(RuntimeException.class, () -> LongArrayMultiStateCounter.CREATOR.createFromParcel(parcel)); } }
core/tests/coretests/src/com/android/internal/os/LongMultiStateCounterTest.java +12 −0 Original line number Diff line number Diff line Loading @@ -18,6 +18,8 @@ package com.android.internal.os; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; import android.os.Parcel; import androidx.test.filters.SmallTest; Loading Loading @@ -198,4 +200,14 @@ public class LongMultiStateCounterTest { assertThat(newCounter.getCount(0)).isEqualTo(116); } @Test public void createFromBadParcel() { // Check we don't crash the runtime if the Parcel data is bad (b/243434675). Parcel parcel = Parcel.obtain(); parcel.writeInt(13); parcel.setDataPosition(0); assertThrows(RuntimeException.class, () -> LongMultiStateCounter.CREATOR.createFromParcel(parcel)); } }