From 31c1beb1d8de373f7734a54e25ad80b01024adb0 Mon Sep 17 00:00:00 2001 From: Christopher Wiley Date: Fri, 19 Aug 2016 11:43:54 -0700 Subject: libbinder: Support reading/writing out T[] lengths T[] types are special: the length of the out array is written into the inbound parcel to signal the expected length to the server. Bug: 30836680 Change-Id: I4f2f39fd7b1a6b94a25bd7f6c3baed430fd567bc Test: integration tests pass with this change. --- include/binder/Parcel.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/include/binder/Parcel.h b/include/binder/Parcel.h index fe3aed6213..7ee25dd19f 100644 --- a/include/binder/Parcel.h +++ b/include/binder/Parcel.h @@ -166,6 +166,10 @@ public: template status_t write(const LightFlattenable& val); + template + status_t writeVectorSize(const std::vector& val); + template + status_t writeVectorSize(const std::unique_ptr>& val); // Place a native_handle into the parcel (the native_handle's file- // descriptors are dup'ed, so it is safe to delete the native_handle @@ -305,6 +309,11 @@ public: template status_t read(LightFlattenable& val) const; + template + status_t resizeOutVector(std::vector* val) const; + template + status_t resizeOutVector(std::unique_ptr>* val) const; + // Like Parcel.java's readExceptionCode(). Reads the first int32 // off of a Parcel's header, returning 0 or the negative error // code on exceptions, but also deals with skipping over rich @@ -563,6 +572,54 @@ status_t Parcel::read(LightFlattenable& val) const { return NO_ERROR; } +template +status_t Parcel::writeVectorSize(const std::vector& val) { + if (val.size() > INT32_MAX) { + return BAD_VALUE; + } + return writeInt32(val.size()); +} + +template +status_t Parcel::writeVectorSize(const std::unique_ptr>& val) { + if (!val) { + return writeInt32(-1); + } + + return writeVectorSize(*val); +} + +template +status_t Parcel::resizeOutVector(std::vector* val) const { + int32_t size; + status_t err = readInt32(&size); + if (err != NO_ERROR) { + return err; + } + + if (size < 0) { + return UNEXPECTED_NULL; + } + val->resize(size_t(size)); + return OK; +} + +template +status_t Parcel::resizeOutVector(std::unique_ptr>* val) const { + int32_t size; + status_t err = readInt32(&size); + if (err != NO_ERROR) { + return err; + } + + val->reset(); + if (size >= 0) { + val->reset(new std::vector(size_t(size))); + } + + return OK; +} + template status_t Parcel::readStrongBinder(sp* val) const { sp tmp; -- cgit v1.2.3-59-g8ed1b