From d117f6f6d50849740bc3b5baac1beb949047f589 Mon Sep 17 00:00:00 2001 From: Hao Ke Date: Mon, 1 Nov 2021 14:46:10 +0000 Subject: [PATCH] Added End-of-Parcel check API. Added End-of-Parcel check API, that verifies there are no bytes left to be read on the Parcel. Test: atest -d android.os.cts.ParcelTest#testEnforceNoDataAvail Bug: 195622897 Bug: 204990745 Change-Id: I3068568d8d4371b071aecd357adeb45a2d7103e4 --- core/api/current.txt | 1 + core/java/android/os/Parcel.java | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/core/api/current.txt b/core/api/current.txt index 99226159348b1..095454a1348a7 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -31470,6 +31470,7 @@ package android.os { method public int dataPosition(); method public int dataSize(); method public void enforceInterface(@NonNull String); + method public void enforceNoDataAvail(); method public boolean hasFileDescriptors(); method public boolean hasFileDescriptors(int, int); method public byte[] marshall(); diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java index afd0ff747b93c..7ef2a9442f30d 100644 --- a/core/java/android/os/Parcel.java +++ b/core/java/android/os/Parcel.java @@ -845,6 +845,19 @@ public final class Parcel { nativeEnforceInterface(mNativePtr, interfaceName); } + /** + * Verify there are no bytes left to be read on the Parcel. + * + * @throws BadParcelableException If the current position hasn't reached the end of the Parcel. + * When used over binder, this exception should propagate to the caller. + */ + public void enforceNoDataAvail() { + final int n = dataAvail(); + if (n > 0) { + throw new BadParcelableException("Parcel data not fully consumed, unread size: " + n); + } + } + /** * Writes the work source uid to the request headers. *