From 1327192d16a64736ad264c8093c67998e78b0eaa Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Thu, 23 Feb 2023 11:01:47 -0500 Subject: [PATCH] Handle an (invalid) empty WriteResponses list. (#25229) Without this change, WriteInteraction could end up not calling either of its callbacks if the server responded to a non-group non-wildcard write with an empty WriteResponses list. --- src/controller/WriteInteraction.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/controller/WriteInteraction.h b/src/controller/WriteInteraction.h index 4926e98b28c471..19e383d738efe8 100644 --- a/src/controller/WriteInteraction.h +++ b/src/controller/WriteInteraction.h @@ -50,8 +50,8 @@ class WriteCallback final : public app::WriteClient::Callback using OnErrorCallbackType = std::function; using OnDoneCallbackType = std::function; - WriteCallback(OnSuccessCallbackType aOnSuccess, OnErrorCallbackType aOnError, OnDoneCallbackType aOnDone) : - mOnSuccess(aOnSuccess), mOnError(aOnError), mOnDone(aOnDone), mCallback(this) + WriteCallback(OnSuccessCallbackType aOnSuccess, OnErrorCallbackType aOnError, OnDoneCallbackType aOnDone, bool aIsGroupWrite) : + mOnSuccess(aOnSuccess), mOnError(aOnError), mOnDone(aOnDone), mIsGroupWrite(aIsGroupWrite), mCallback(this) {} app::WriteClient::Callback * GetChunkedCallback() { return &mCallback; } @@ -88,6 +88,16 @@ class WriteCallback final : public app::WriteClient::Callback void OnDone(app::WriteClient * apWriteClient) override { + if (!mIsGroupWrite && !mCalledCallback) + { + // This can happen if the server sends a response with an empty + // WriteResponses list. Since we are not sending wildcard write + // paths, that's not a valid response and we should treat it as an + // error. Use the error we would have gotten if we in fact expected + // a nonempty list. + OnError(apWriteClient, CHIP_END_OF_TLV); + } + if (mOnDone != nullptr) { mOnDone(apWriteClient); @@ -104,6 +114,7 @@ class WriteCallback final : public app::WriteClient::Callback OnDoneCallbackType mOnDone = nullptr; bool mCalledCallback = false; + bool mIsGroupWrite = false; app::ChunkedWriteCallback mCallback; }; @@ -121,7 +132,7 @@ CHIP_ERROR WriteAttribute(const SessionHandle & sessionHandle, chip::EndpointId WriteCallback::OnDoneCallbackType onDoneCb = nullptr, const Optional & aDataVersion = NullOptional) { - auto callback = Platform::MakeUnique(onSuccessCb, onErrorCb, onDoneCb); + auto callback = Platform::MakeUnique(onSuccessCb, onErrorCb, onDoneCb, sessionHandle->IsGroupSession()); VerifyOrReturnError(callback != nullptr, CHIP_ERROR_NO_MEMORY); auto client = Platform::MakeUnique(app::InteractionModelEngine::GetInstance()->GetExchangeManager(),