Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure we propagate TLV decode errors to consumers in Darwin framework. #26890

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/darwin/Framework/CHIP/MTRBaseDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ id _Nullable MTRDecodeDataValueDictionaryFromCHIPTLV(chip::TLV::TLVReader * data
chip::TLV::Tag tag = data->GetTag();
id value = MTRDecodeDataValueDictionaryFromCHIPTLV(data);
if (value == nullptr) {
MTR_LOG_ERROR("Error when decoding TLV container");
MTR_LOG_ERROR("Error when decoding TLV container of type %s", typeName.UTF8String);
return nil;
}
NSMutableDictionary * arrayElement = [NSMutableDictionary dictionary];
Expand Down
22 changes: 15 additions & 7 deletions src/darwin/Framework/CHIP/MTRDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1356,14 +1356,16 @@ - (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_INVALID_ARGUMENT]
}];
} else {
id value;
if (apData == nullptr) {
value = nil;
id value = MTRDecodeDataValueDictionaryFromCHIPTLV(apData);
if (value == nil) {
MTR_LOG_ERROR("Failed to decode event data for path %@", eventPath);
[mEventReports addObject:@ {
MTREventPathKey : eventPath,
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_DECODE_FAILED],
}];
} else {
value = MTRDecodeDataValueDictionaryFromCHIPTLV(apData);
[mEventReports addObject:[MTRBaseDevice eventReportForHeader:aEventHeader andData:value]];
}

[mEventReports addObject:[MTRBaseDevice eventReportForHeader:aEventHeader andData:value]];
}
}

Expand Down Expand Up @@ -1391,7 +1393,13 @@ - (void)invokeCommandWithEndpointID:(NSNumber *)endpointID
}];
} else {
id value = MTRDecodeDataValueDictionaryFromCHIPTLV(apData);
if (value) {
if (value == nil) {
MTR_LOG_ERROR("Failed to decode attribute data for path %@", attributePath);
[mAttributeReports addObject:@ {
MTRAttributePathKey : attributePath,
MTRErrorKey : [MTRError errorForCHIPErrorCode:CHIP_ERROR_DECODE_FAILED],
}];
} else {
[mAttributeReports addObject:@ { MTRAttributePathKey : attributePath, MTRDataKey : value }];
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/darwin/Framework/CHIP/MTRError.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ typedef NS_ERROR_ENUM(MTRErrorDomain, MTRErrorCode){
* expected schema.
*/
MTRErrorCodeSchemaMismatch MTR_NEWLY_AVAILABLE = 13,
/**
* MTRErrorCodeTLVDecodeFailed means that the TLV being decoded was malformed in
* some way. This can include things like lengths running past the end of
* the buffer, strings that are not actually UTF-8, and various other
* TLV-level failures.
*/
MTRErrorCodeTLVDecodeFailed MTR_NEWLY_AVAILABLE = 14,
};
// clang-format on

Expand Down
3 changes: 3 additions & 0 deletions src/darwin/Framework/CHIP/MTRError.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ + (NSError *)errorForCHIPErrorCode:(CHIP_ERROR)errorCode
[userInfo addEntriesFromDictionary:@{
NSLocalizedDescriptionKey : NSLocalizedString(@"The device is already a member of this fabric.", nil)
}];
} else if (errorCode == CHIP_ERROR_DECODE_FAILED) {
code = MTRErrorCodeTLVDecodeFailed;
[userInfo addEntriesFromDictionary:@{ NSLocalizedDescriptionKey : NSLocalizedString(@"TLV decoding failed.", nil) }];
} else {
code = MTRErrorCodeGeneralError;
[userInfo addEntriesFromDictionary:@{
Expand Down