From 15d306eb69e02cdde3a622d9c5cca0d3601fb2b3 Mon Sep 17 00:00:00 2001 From: rob-the-dude <43481893+rob-the-dude@users.noreply.github.com> Date: Sun, 16 Apr 2023 21:04:29 -0700 Subject: [PATCH] Fix #26115: Setup discriminator parameter corruption (#26116) * Fix #26115: Setup discriminator parameter corruption The setup discriminator parameter is being passed by reference, but by the time the block is run by dispatch, that reference points to stack contents that have been changed. Making a local copy of the discriminator fixes the problem. * Add comment explaining why we are copying the incoming discriminator. --------- Co-authored-by: Boris Zbarsky --- src/platform/Darwin/BleConnectionDelegateImpl.mm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/platform/Darwin/BleConnectionDelegateImpl.mm b/src/platform/Darwin/BleConnectionDelegateImpl.mm index 9f05bb65edae16..ab8242ea649060 100644 --- a/src/platform/Darwin/BleConnectionDelegateImpl.mm +++ b/src/platform/Darwin/BleConnectionDelegateImpl.mm @@ -91,10 +91,13 @@ - (BOOL)isConnecting; dispatch_queue_t bleWorkQueue; void BleConnectionDelegateImpl::NewConnection( - Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & deviceDiscriminator) + Ble::BleLayer * bleLayer, void * appState, const SetupDiscriminator & inDeviceDiscriminator) { assertChipStackLockedByCurrentThread(); + // Make a copy of the device discriminator for the block to capture. + SetupDiscriminator deviceDiscriminator = inDeviceDiscriminator; + ChipLogProgress(Ble, "%s", __FUNCTION__); if (!bleWorkQueue) { bleWorkQueue = dispatch_queue_create(kBleWorkQueueName, DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);