Skip to content

Commit 72be694

Browse files
committed
[Issue #2285] Fixing potential crash caused by failed validation with nil responseData
1 parent c81ab7f commit 72be694

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

AFNetworking/AFURLResponseSerialization.m

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,28 +114,33 @@ - (BOOL)validateResponse:(NSHTTPURLResponse *)response
114114
if (response && [response isKindOfClass:[NSHTTPURLResponse class]]) {
115115
if (self.acceptableContentTypes && ![self.acceptableContentTypes containsObject:[response MIMEType]]) {
116116
if ([data length] > 0) {
117-
NSDictionary *userInfo = @{
118-
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
119-
NSURLErrorFailingURLErrorKey:[response URL],
120-
AFNetworkingOperationFailingURLResponseErrorKey: response,
121-
AFNetworkingOperationFailingURLResponseDataErrorKey: data
122-
};
117+
NSMutableDictionary *mutableUserInfo = [@{
118+
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: unacceptable content-type: %@", @"AFNetworking", nil), [response MIMEType]],
119+
NSURLErrorFailingURLErrorKey:[response URL],
120+
AFNetworkingOperationFailingURLResponseErrorKey: response,
121+
} mutableCopy];
122+
if (data) {
123+
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
124+
}
123125

124-
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:userInfo], validationError);
126+
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorCannotDecodeContentData userInfo:mutableUserInfo], validationError);
125127
}
126128

127129
responseIsValid = NO;
128130
}
129131

130132
if (self.acceptableStatusCodes && ![self.acceptableStatusCodes containsIndex:(NSUInteger)response.statusCode]) {
131-
NSDictionary *userInfo = @{
132-
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
133-
NSURLErrorFailingURLErrorKey:[response URL],
134-
AFNetworkingOperationFailingURLResponseErrorKey: response,
135-
AFNetworkingOperationFailingURLResponseDataErrorKey: data
136-
};
137-
138-
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo], validationError);
133+
NSMutableDictionary *mutableUserInfo = [@{
134+
NSLocalizedDescriptionKey: [NSString stringWithFormat:NSLocalizedStringFromTable(@"Request failed: %@ (%ld)", @"AFNetworking", nil), [NSHTTPURLResponse localizedStringForStatusCode:response.statusCode], (long)response.statusCode],
135+
NSURLErrorFailingURLErrorKey:[response URL],
136+
AFNetworkingOperationFailingURLResponseErrorKey: response,
137+
} mutableCopy];
138+
139+
if (data) {
140+
mutableUserInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] = data;
141+
}
142+
143+
validationError = AFErrorWithUnderlyingError([NSError errorWithDomain:AFURLResponseSerializationErrorDomain code:NSURLErrorBadServerResponse userInfo:mutableUserInfo], validationError);
139144

140145
responseIsValid = NO;
141146
}

0 commit comments

Comments
 (0)