From 8aee64d18b6b90e46e886a80d8de0467d682ddc4 Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Fri, 25 Oct 2013 10:41:50 -0700 Subject: [PATCH] Fix issue #11290095: Parcel change causing crashing in KLP... ...when reading from empty byte[] Better handling of empty bundles and parcels. Change-Id: Ie9182e43a52b8f163e8fb20bd1d110e0832683e6 --- core/java/android/os/Bundle.java | 56 +++++++++++++++++++++++++++----- core/java/android/os/Parcel.java | 15 ++++++--- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/core/java/android/os/Bundle.java b/core/java/android/os/Bundle.java index 5a919fb8209e2..f9c1d318a9f69 100644 --- a/core/java/android/os/Bundle.java +++ b/core/java/android/os/Bundle.java @@ -35,10 +35,12 @@ public final class Bundle implements Parcelable, Cloneable { public static final Bundle EMPTY; static final int BUNDLE_MAGIC = 0x4C444E42; // 'B' 'N' 'D' 'L' + static final Parcel EMPTY_PARCEL; static { EMPTY = new Bundle(); EMPTY.mMap = ArrayMap.EMPTY; + EMPTY_PARCEL = Parcel.obtain(); } // Invariant - exactly one of mMap / mParcelledData will be null @@ -115,9 +117,13 @@ public final class Bundle implements Parcelable, Cloneable { */ public Bundle(Bundle b) { if (b.mParcelledData != null) { - mParcelledData = Parcel.obtain(); - mParcelledData.appendFrom(b.mParcelledData, 0, b.mParcelledData.dataSize()); - mParcelledData.setDataPosition(0); + if (b.mParcelledData == EMPTY_PARCEL) { + mParcelledData = EMPTY_PARCEL; + } else { + mParcelledData = Parcel.obtain(); + mParcelledData.appendFrom(b.mParcelledData, 0, b.mParcelledData.dataSize()); + mParcelledData.setDataPosition(0); + } } else { mParcelledData = null; } @@ -216,6 +222,18 @@ public final class Bundle implements Parcelable, Cloneable { return; } + if (mParcelledData == EMPTY_PARCEL) { + if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this)) + + ": empty"); + if (mMap == null) { + mMap = new ArrayMap(1); + } else { + mMap.erase(); + } + mParcelledData = null; + return; + } + int N = mParcelledData.readInt(); if (DEBUG) Log.d(TAG, "unparcel " + Integer.toHexString(System.identityHashCode(this)) + ": reading " + N + " maps"); @@ -1652,11 +1670,20 @@ public final class Bundle implements Parcelable, Cloneable { final boolean oldAllowFds = parcel.pushAllowFds(mAllowFds); try { if (mParcelledData != null) { - int length = mParcelledData.dataSize(); - parcel.writeInt(length); - parcel.writeInt(BUNDLE_MAGIC); - parcel.appendFrom(mParcelledData, 0, length); + if (mParcelledData == EMPTY_PARCEL) { + parcel.writeInt(0); + } else { + int length = mParcelledData.dataSize(); + parcel.writeInt(length); + parcel.writeInt(BUNDLE_MAGIC); + parcel.appendFrom(mParcelledData, 0, length); + } } else { + // Special case for empty bundles. + if (mMap == null || mMap.size() <= 0) { + parcel.writeInt(0); + return; + } int lengthPos = parcel.dataPosition(); parcel.writeInt(-1); // dummy, will hold length parcel.writeInt(BUNDLE_MAGIC); @@ -1690,6 +1717,13 @@ public final class Bundle implements Parcelable, Cloneable { } void readFromParcelInner(Parcel parcel, int length) { + if (length == 0) { + // Empty Bundle or end of data. + mParcelledData = EMPTY_PARCEL; + mHasFds = false; + mFdsKnown = true; + return; + } int magic = parcel.readInt(); if (magic != BUNDLE_MAGIC) { //noinspection ThrowableInstanceNeverThrown @@ -1716,8 +1750,12 @@ public final class Bundle implements Parcelable, Cloneable { @Override public synchronized String toString() { if (mParcelledData != null) { - return "Bundle[mParcelledData.dataSize=" + - mParcelledData.dataSize() + "]"; + if (mParcelledData == EMPTY_PARCEL) { + return "Bundle[EMPTY_PARCEL]"; + } else { + return "Bundle[mParcelledData.dataSize=" + + mParcelledData.dataSize() + "]"; + } } return "Bundle[" + mMap.toString() + "]"; } diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java index 02b1998264a2e..94b961793e267 100644 --- a/core/java/android/os/Parcel.java +++ b/core/java/android/os/Parcel.java @@ -611,11 +611,15 @@ public final class Parcel { here.fillInStackTrace(); Log.d(TAG, "Writing " + N + " ArrayMap entries", here); } + int startPos; for (int i=0; i 0) { + if (DEBUG_ARRAY_MAP) startPos = dataPosition(); Object key = readValue(loader); - if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + ": key=0x" - + (key != null ? key.hashCode() : 0) + " " + key); Object value = readValue(loader); + if (DEBUG_ARRAY_MAP) Log.d(TAG, " Read #" + (N-1) + " " + + (dataPosition()-startPos) + " bytes: key=0x" + + Integer.toHexString((key != null ? key.hashCode() : 0)) + " " + key); outVal.append(key, value); N--; }