Skip to content

Commit 4402343

Browse files
authored
Fix EXC_BAD_ACCESS (#402)
• Fixes an issue where improperly formatted button JSON would cause the SDK to insert nil objects into an NSDictionary instance, causing an EXC_BAD_ACCESS • Adds a check to validate button format before attempting to parse it
1 parent 8242f97 commit 4402343

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

iOS_SDK/OneSignalSDK/Source/OSNotificationPayload.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,18 @@ -(void)parseRemoteSlient:(NSDictionary*)payload {
154154
-(void)parseActionButtons:(NSArray<NSDictionary*>*)buttons {
155155
NSMutableArray *buttonArray = [NSMutableArray new];
156156
for (NSDictionary *button in buttons) {
157-
[buttonArray addObject: @{@"text" : button[@"n"],
158-
@"id" : (button[@"i"] ?: button[@"n"])
159-
}];
157+
158+
// check to ensure the button object has the correct
159+
// format before adding it to the array
160+
if (!button[@"n"] ||
161+
(!button[@"i"] && !button[@"n"])) {
162+
continue;
163+
}
164+
165+
[buttonArray addObject: @{
166+
@"text" : button[@"n"],
167+
@"id" : (button[@"i"] ?: button[@"n"])
168+
}];
160169
}
161170

162171
_actionButtons = buttonArray;

iOS_SDK/OneSignalSDK/UnitTests/UnitTests.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,4 +2051,22 @@ - (void)testRegistersAfterNoApnsResponse {
20512051
XCTAssertTrue(observer->last.to.userId != nil);
20522052
}
20532053

2054+
// Regression test to ensure improper button JSON format
2055+
// does not cause a crash (iOS SDK issue #401)
2056+
- (void)testInvalidButtonFormat {
2057+
NSDictionary *newFormat = @{@"aps": @{
2058+
@"mutable-content": @1,
2059+
@"alert": @"Message Body"
2060+
},
2061+
@"os_data": @{
2062+
@"i": @"b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
2063+
@"buttons": @[@{@"i": @"id1", @"title": @"text1"}],
2064+
@"att": @{ @"id": @"http://domain.com/file.jpg" }
2065+
}};
2066+
2067+
let notification = [OSNotificationPayload parseWithApns:newFormat];
2068+
2069+
XCTAssertTrue(notification.actionButtons.count == 0);
2070+
}
2071+
20542072
@end

0 commit comments

Comments
 (0)