From 3c72e4f40ca1694b7a082178142c36bf084a1573 Mon Sep 17 00:00:00 2001 From: Nick Bourdakos Date: Sun, 21 May 2023 19:18:07 -0700 Subject: [PATCH] Fix `EXC_BAD_ACCESS` crash when calling payload delegate methods. Objective-C objects cannot be captured by reference in C++ lambdas and must always be captured by value. This also takes a copy of the data returned from the Wi-Fi LAN reader, to prevent potential intermittent empty packet issues. Fixes: #1702 PiperOrigin-RevId: 533925980 --- connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm | 4 ++-- .../implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm b/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm index 8a55cd612a..716424e699 100644 --- a/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm +++ b/connections/swift/NearbyCoreAdapter/Sources/GNCCoreAdapter.mm @@ -278,13 +278,13 @@ - (void)acceptConnectionRequestFromEndpoint:(NSString *)endpointID std::string endpoint_id = [endpointID cStringUsingEncoding:[NSString defaultCStringEncoding]]; PayloadListener listener; - listener.payload_cb = [&delegate](absl::string_view endpoint_id, Payload payload) { + listener.payload_cb = [delegate](absl::string_view endpoint_id, Payload payload) { NSString *endpointID = @(std::string(endpoint_id).c_str()); GNCPayload *gncPayload = [GNCPayload fromCpp:std::move(payload)]; [delegate receivedPayload:gncPayload fromEndpoint:endpointID]; }; listener.payload_progress_cb = - [&delegate](absl::string_view endpoint_id, const PayloadProgressInfo &info) { + [delegate](absl::string_view endpoint_id, const PayloadProgressInfo &info) { NSString *endpointID = @(std::string(endpoint_id).c_str()); GNCPayloadStatus status; switch (info.status) { diff --git a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m index 427d4e8fa3..62744ca775 100644 --- a/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m +++ b/internal/platform/implementation/apple/Mediums/WiFiLAN/GNCWiFiLANSocket.m @@ -57,7 +57,7 @@ - (NSData *)readMaxLength:(NSUInteger)length error:(NSError **)error { } #if __LP64__ // This cast is only safe in a 64-bit runtime. - blockResult = (NSData *)content; + blockResult = [(NSData *)content copy]; #else blockResult = nil; #endif