Skip to content

Commit f66342e

Browse files
authored
Merge pull request #919 from BranchMetrics/INTENG-6187-bad-access-crash-
INTENG-6187 bad access crash
2 parents 444ca3f + bc52998 commit f66342e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Branch-SDK/Branch-SDK/BNCEncodingUtils.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ + (NSData *)encodeDictionaryToJsonData:(NSDictionary *)dictionary {
164164
+ (NSString *)encodeDictionaryToJsonString:(NSDictionary *)dictionary {
165165
NSMutableString *encodedDictionary = [[NSMutableString alloc] initWithString:@"{"];
166166
for (NSString *key in dictionary) {
167+
168+
// protect against non-string keys
169+
if (![key isKindOfClass:[NSString class]]) {
170+
BNCLogError(@"Unexpected key type %@. Skipping key.", [key class]);
171+
continue;
172+
}
173+
167174
NSString *value = nil;
168175
BOOL string = YES;
169176

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// BNCEncodingUtilsTests.m
3+
// Branch-SDK-Unhosted-Tests
4+
//
5+
// Created by Ernest Cho on 5/17/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCEncodingUtils.h"
11+
12+
@interface BNCEncodingUtilsTests : XCTestCase
13+
14+
@end
15+
16+
@implementation BNCEncodingUtilsTests
17+
18+
- (void)setUp {
19+
// Put setup code here. This method is called before the invocation of each test method in the class.
20+
}
21+
22+
- (void)tearDown {
23+
// Put teardown code here. This method is called after the invocation of each test method in the class.
24+
}
25+
26+
- (void) testSanitzeString {
27+
NSString*test = @"\b\f\n\r\t\"`\\";
28+
NSString*truth = @"\\b\\f\\n\\r\\t\\\"'\\\\";
29+
NSString*result = [BNCEncodingUtils sanitizedStringFromString:test];
30+
XCTAssertEqualObjects(result, truth);
31+
}
32+
33+
- (void)testSanitizeStringWithNil {
34+
NSString *result = [BNCEncodingUtils sanitizedStringFromString:nil];
35+
XCTAssertNil(result);
36+
}
37+
38+
// INTENG-6187 bad access crash
39+
- (void)testEncodeDictionaryToJsonStringCrashTest {
40+
NSString *expectedString = @"{\"World\":\"Hello\"}";
41+
42+
// untyped collection
43+
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
44+
[params setValue:@"Hello" forKey:@"World"];
45+
[params setObject:@"Goodbye" forKey:@(100)];
46+
47+
// encodeDictionaryToJsonString should ignore non-string keys
48+
NSString *result = [BNCEncodingUtils encodeDictionaryToJsonString:params];
49+
XCTAssertTrue([expectedString isEqualToString:result]);
50+
}
51+
52+
@end

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
54391A161BA249FA0061CB0F /* BranchCSSearchableItemAttributeSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 54391A141BA249FA0061CB0F /* BranchCSSearchableItemAttributeSet.m */; };
167167
54FF1F8E1BD1D4AE0004CE2E /* BranchUniversalObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 54FF1F8C1BD1D4AE0004CE2E /* BranchUniversalObject.m */; };
168168
54FF1F921BD1DC320004CE2E /* BranchLinkProperties.m in Sources */ = {isa = PBXBuildFile; fileRef = 54FF1F901BD1DC320004CE2E /* BranchLinkProperties.m */; };
169+
5F67F48E228F535500067429 /* BNCEncodingUtilsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F67F48D228F535500067429 /* BNCEncodingUtilsTests.m */; };
169170
5F8B7B4021B5F5CD009CE0A6 /* libBranch.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 466B58381B17773000A69EDE /* libBranch.a */; };
170171
5F8B7B4721B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */; };
171172
63E4C4881D25E16A00A45FD8 /* LogOutputViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E4C4871D25E16A00A45FD8 /* LogOutputViewController.m */; };
@@ -388,6 +389,7 @@
388389
54FF1F8C1BD1D4AE0004CE2E /* BranchUniversalObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BranchUniversalObject.m; sourceTree = "<group>"; };
389390
54FF1F8F1BD1DC320004CE2E /* BranchLinkProperties.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BranchLinkProperties.h; sourceTree = "<group>"; };
390391
54FF1F901BD1DC320004CE2E /* BranchLinkProperties.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BranchLinkProperties.m; sourceTree = "<group>"; };
392+
5F67F48D228F535500067429 /* BNCEncodingUtilsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCEncodingUtilsTests.m; sourceTree = "<group>"; };
391393
5F8B7B3B21B5F5CD009CE0A6 /* Branch-SDK-Unhosted-Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Branch-SDK-Unhosted-Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
392394
5F8B7B3F21B5F5CD009CE0A6 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
393395
5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Branch_setBranchKeyTests.m; sourceTree = "<group>"; };
@@ -647,6 +649,7 @@
647649
5F8B7B3C21B5F5CD009CE0A6 /* Branch-SDK-Unhosted-Tests */ = {
648650
isa = PBXGroup;
649651
children = (
652+
5F67F48D228F535500067429 /* BNCEncodingUtilsTests.m */,
650653
5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */,
651654
5F8B7B3F21B5F5CD009CE0A6 /* Info.plist */,
652655
);
@@ -1066,6 +1069,7 @@
10661069
developmentRegion = English;
10671070
hasScannedForEncodings = 0;
10681071
knownRegions = (
1072+
English,
10691073
en,
10701074
Base,
10711075
);
@@ -1212,6 +1216,7 @@
12121216
isa = PBXSourcesBuildPhase;
12131217
buildActionMask = 2147483647;
12141218
files = (
1219+
5F67F48E228F535500067429 /* BNCEncodingUtilsTests.m in Sources */,
12151220
5F8B7B4721B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m in Sources */,
12161221
);
12171222
runOnlyForDeploymentPostprocessing = 0;

0 commit comments

Comments
 (0)