Skip to content

Commit 5f29159

Browse files
Fix use-after-free in CommissioningWindowOpener. (#22767) (#22849)
Once we call back into our client, it can delete us, so we need to do any logging that uses `mSetupPayload` before we do that. Fixes project-chip/connectedhomeip#22765 Co-authored-by: Boris Zbarsky <[email protected]>
1 parent 062f85c commit 5f29159

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/controller/CommissioningWindowOpener.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ void CommissioningWindowOpener::OnOpenCommissioningWindowSuccess(void * context,
204204
self->mNextStep = Step::kAcceptCommissioningStart;
205205
if (self->mCommissioningWindowCallback != nullptr)
206206
{
207-
self->mCommissioningWindowCallback->mCall(self->mCommissioningWindowCallback->mContext, self->mNodeId, CHIP_NO_ERROR,
208-
self->mSetupPayload);
209-
210207
char payloadBuffer[QRCodeBasicSetupPayloadGenerator::kMaxQRCodeBase38RepresentationLength + 1];
211208

212209
MutableCharSpan manualCode(payloadBuffer);
@@ -230,11 +227,18 @@ void CommissioningWindowOpener::OnOpenCommissioningWindowSuccess(void * context,
230227
{
231228
ChipLogError(Controller, "Unable to generate QR code for setup payload: %" CHIP_ERROR_FORMAT, err.Format());
232229
}
230+
231+
self->mCommissioningWindowCallback->mCall(self->mCommissioningWindowCallback->mContext, self->mNodeId, CHIP_NO_ERROR,
232+
self->mSetupPayload);
233+
// Don't touch `self` anymore; it might have been destroyed by the
234+
// callee.
233235
}
234236
else if (self->mBasicCommissioningWindowCallback != nullptr)
235237
{
236238
self->mBasicCommissioningWindowCallback->mCall(self->mBasicCommissioningWindowCallback->mContext, self->mNodeId,
237239
CHIP_NO_ERROR);
240+
// Don't touch `self` anymore; it might have been destroyed by the
241+
// callee.
238242
}
239243
}
240244

0 commit comments

Comments
 (0)