Skip to content

Commit

Permalink
Merge pull request #941 from wutschel/fix_rpc_crash
Browse files Browse the repository at this point in the history
Improved robustness against mal-formatted JSON API responses
  • Loading branch information
kambala-decapitator committed Oct 21, 2023
2 parents f7b6f31 + 0d7b8f9 commit 8700af2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions XBMC Remote/dbowen-Demiurgic-JSON-RPC-168ecc9/DSJSONRPC.m
Expand Up @@ -184,6 +184,20 @@ - (void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task didComple
completionHandler(methodName, aID, nil, nil, aError);
}
}
// Deserialized object is not a dictionary
else if (![jsonResult isKindOfClass:[NSDictionary class]]) {
// Pass the error to completion handler
if (completionHandler) {
NSError *aError = [NSError errorWithDomain:RPC_DOMAIN code:DSJSONRPCParseError userInfo:@{NSLocalizedDescriptionKey: LOCALIZED_STR(@"Received unexpected JSON object.")}];
NSDictionary *jsonErrorDict = @{
@"code": @(JSONRPCInvalidObject),
@"message": LOCALIZED_STR(@"Received unexpected JSON object."),
@"data": [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding],
};
DSJSONRPCError *jsonRPCError = [DSJSONRPCError errorWithData:jsonErrorDict];
completionHandler(methodName, aID, nil, jsonRPCError, aError);
}
}
// The JSON server passed back an error for the response
else if (!jsonError && jsonResult[@"error"] != nil && [jsonResult[@"error"] isKindOfClass:[NSDictionary class]]) {
// Pass the error to completion handler
Expand Down
Expand Up @@ -43,7 +43,8 @@ typedef enum {
JSONRPCInvalidRequest = -32600,
JSONRPCMethodNotFound = -32601,
JSONRPCInvalidParams = -32602,
JSONRPCInternalError = -32603
JSONRPCInternalError = -32603,
JSONRPCInvalidObject = -32604,
} JSONRPCErrorType;


Expand Down
2 changes: 2 additions & 0 deletions XBMC Remote/en.lproj/Localizable.strings
Expand Up @@ -362,3 +362,5 @@
"Metering mode" = "Metering mode";
"Focal length" = "Focal length";
"Camera model" = "Camera model";

"Received unexpected JSON object." = "Received unexpected JSON object.";

0 comments on commit 8700af2

Please sign in to comment.