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

Commit 4ce59c78 authored by Steven Moreland's avatar Steven Moreland
Browse files

libbinder_ndk: null ParcelFileDescriptor

-1 <=> nullptr

Bug: 111445392
Test: atest android.binder.cts
Change-Id: I1047ab7f6997672278988ef0079927c1d8a773f5
parent fe031c88
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -347,7 +347,7 @@ binder_status_t AParcel_readStrongBinder(const AParcel* parcel, AIBinder** binde
 * This corresponds to the SDK's android.os.ParcelFileDescriptor.
 *
 * \param parcel the parcel to write to.
 * \param fd the value to write to the parcel.
 * \param fd the value to write to the parcel (-1 to represent a null ParcelFileDescriptor).
 *
 * \return STATUS_OK on successful write.
 */
@@ -361,7 +361,8 @@ binder_status_t AParcel_writeParcelFileDescriptor(AParcel* parcel, int fd);
 * This corresponds to the SDK's android.os.ParcelFileDescriptor.
 *
 * \param parcel the parcel to read from.
 * \param binder the out parameter for what is read from the parcel.
 * \param fd the out parameter for what is read from the parcel (or -1 to represent a null
 * ParcelFileDescriptor)
 *
 * \return STATUS_OK on successful write.
 */
+48 −0
Original line number Diff line number Diff line
@@ -201,6 +201,54 @@ static inline binder_status_t AParcel_readRequiredStrongBinder(const AParcel* pa
    return ret;
}

/**
 * Convenience method to write a ParcelFileDescriptor where -1 represents a null value.
 */
static inline binder_status_t AParcel_writeNullableParcelFileDescriptor(
        AParcel* parcel, const ScopedFileDescriptor& fd) {
    return AParcel_writeParcelFileDescriptor(parcel, fd.get());
}

/**
 * Convenience method to read a ParcelFileDescriptor where -1 represents a null value.
 */
static inline binder_status_t AParcel_readNullableParcelFileDescriptor(const AParcel* parcel,
                                                                       ScopedFileDescriptor* fd) {
    int readFd;
    binder_status_t status = AParcel_readParcelFileDescriptor(parcel, &readFd);
    if (status == STATUS_OK) {
        fd->set(readFd);
    }
    return status;
}

/**
 * Convenience method to write a valid ParcelFileDescriptor.
 */
static inline binder_status_t AParcel_writeRequiredParcelFileDescriptor(
        AParcel* parcel, const ScopedFileDescriptor& fd) {
    if (fd.get() < 0) {
        return STATUS_UNEXPECTED_NULL;
    }
    return AParcel_writeParcelFileDescriptor(parcel, fd.get());
}

/**
 * Convenience method to read a valid ParcelFileDescriptor.
 */
static inline binder_status_t AParcel_readRequiredParcelFileDescriptor(const AParcel* parcel,
                                                                       ScopedFileDescriptor* fd) {
    int readFd;
    binder_status_t status = AParcel_readParcelFileDescriptor(parcel, &readFd);
    if (status == STATUS_OK) {
        if (readFd < 0) {
            return STATUS_UNEXPECTED_NULL;
        }
        fd->set(readFd);
    }
    return status;
}

/**
 * Allocates a std::string to length and returns the underlying buffer. For use with
 * AParcel_readString. See use below in AParcel_readString(const AParcel*, std::string*).
+22 −6
Original line number Diff line number Diff line
@@ -229,23 +229,39 @@ binder_status_t AParcel_readStrongBinder(const AParcel* parcel, AIBinder** binde
}

binder_status_t AParcel_writeParcelFileDescriptor(AParcel* parcel, int fd) {
    ParcelFileDescriptor parcelFd((unique_fd(fd)));
    std::unique_ptr<ParcelFileDescriptor> parcelFd;

    status_t status = parcel->get()->writeParcelable(parcelFd);
    if (fd < 0) {
        if (fd != -1) {
            return STATUS_UNKNOWN_ERROR;
        }
        // parcelFd = nullptr
    } else {  // fd >= 0
        parcelFd = std::make_unique<ParcelFileDescriptor>(unique_fd(fd));
    }

    status_t status = parcel->get()->writeNullableParcelable(parcelFd);

    // ownership is retained by caller
    (void)parcelFd.release().release();
    if (parcelFd != nullptr) {
        (void)parcelFd->release().release();
    }

    return PruneStatusT(status);
}

binder_status_t AParcel_readParcelFileDescriptor(const AParcel* parcel, int* fd) {
    ParcelFileDescriptor parcelFd;
    // status_t status = parcelFd.readFromParcel(parcel->get());
    std::unique_ptr<ParcelFileDescriptor> parcelFd;

    status_t status = parcel->get()->readParcelable(&parcelFd);
    if (status != STATUS_OK) return PruneStatusT(status);

    *fd = parcelFd.release().release();
    if (parcelFd) {
        *fd = parcelFd->release().release();
    } else {
        *fd = -1;
    }

    return STATUS_OK;
}