Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 10ae93b7 authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder_ndk Parcelable: no assume non-null write

This is in preparation for supporting @nullable parcelables. Before, for
a parcelable, P::readFromParcel always reads a one. However, this means
that from a 'read nullable parcel' method, we would need to peek the
next int to determine whether or not we want to read the full parcelable
or just return null. In order to avoid this peeking, moving the read of
this int32_t to AParcel_readParcelable. For symmetry,
AParcel_writeParcelable is also added.

Bug: 146172425
Test: atest CtsNdkBinderTestCases
Change-Id: Ie8209979bfd8a2c5b95d129025e5ba5a40c58b9e
parent 5d4883d8
Loading
Loading
Loading
Loading
+30 −2
Original line number Diff line number Diff line
@@ -420,6 +420,34 @@ static inline binder_status_t AParcel_readVector(
            AParcel_nullableStdVectorStringElementAllocator);
}

/**
 * Convenience API for writing a non-null parcelable.
 */
template <typename P>
static inline binder_status_t AParcel_writeParcelable(AParcel* parcel, const P& p) {
    binder_status_t status = AParcel_writeInt32(parcel, 1);  // non-null
    if (status != STATUS_OK) {
        return status;
    }
    return p.writeToParcel(parcel);
}

/**
 * Convenience API for reading a non-null parcelable.
 */
template <typename P>
static inline binder_status_t AParcel_readParcelable(const AParcel* parcel, P* p) {
    int32_t null;
    binder_status_t status = AParcel_readInt32(parcel, &null);
    if (status != STATUS_OK) {
        return status;
    }
    if (null == 0) {
        return STATUS_UNEXPECTED_NULL;
    }
    return p->readFromParcel(parcel);
}

/**
 * Writes a parcelable object of type P inside a std::vector<P> at index 'index' to 'parcel'.
 */
@@ -427,7 +455,7 @@ template <typename P>
binder_status_t AParcel_writeStdVectorParcelableElement(AParcel* parcel, const void* vectorData,
                                                        size_t index) {
    const std::vector<P>* vector = static_cast<const std::vector<P>*>(vectorData);
    return vector->at(index).writeToParcel(parcel);
    return AParcel_writeParcelable(parcel, vector->at(index));
}

/**
@@ -437,7 +465,7 @@ template <typename P>
binder_status_t AParcel_readStdVectorParcelableElement(const AParcel* parcel, void* vectorData,
                                                       size_t index) {
    std::vector<P>* vector = static_cast<std::vector<P>*>(vectorData);
    return vector->at(index).readFromParcel(parcel);
    return AParcel_readParcelable(parcel, &vector->at(index));
}

/**