diff --git a/FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.m b/FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.m index 9bb4a1c46f2..2b72535833c 100644 --- a/FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.m +++ b/FirebasePerformance/Sources/Instrumentation/FPRNetworkTrace.m @@ -251,8 +251,17 @@ - (void)didCompleteRequestWithResponse:(NSURLResponse *)response error:(NSError self.responseCode = (int32_t)HTTPResponse.statusCode; } self.responseError = error; - // Safely copy MIMEType to prevent use after free - NSString *mime = [response.MIMEType copy]; + // Defensive access to MIMEType + NSString *mime = nil; + @try { + // Safely copy MIMEType to prevent use after free + mime = [response.MIMEType copy]; + } @catch (NSException *exception) { + FPRLogWarning(@"MIMETypeException", + @"Exception while accessing MIMEType for URL %@: %@. Trace will continue " + @"without MIMEType.", + self.URLRequest.URL, exception); + } self.responseContentType = (mime.length ? mime : nil); [self checkpointState:FPRNetworkTraceCheckpointStateResponseCompleted];