Skip to content

Commit 34d00ff

Browse files
authored
Disregard payloads that fail conversion from plist to json backing (#925)
1 parent abc43e6 commit 34d00ff

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Analytics/Internal/SEGFileStorage.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,18 @@ - (id _Nullable)jsonForKey:(NSString *)key
160160
BOOL needsConversion = NO;
161161
result = [self jsonFromData:data needsConversion:&needsConversion];
162162
if (needsConversion) {
163-
[self setJSON:result forKey:key];
164-
// maybe a little repetitive, but we want to recreate the same path it would
165-
// take if it weren't being converted.
166-
data = [self dataForKey:key];
167-
result = [self jsonFromData:data needsConversion:&needsConversion];
163+
@try {
164+
[self setJSON:result forKey:key];
165+
// maybe a little repetitive, but we want to recreate the same path it would
166+
// take if it weren't being converted.
167+
data = [self dataForKey:key];
168+
result = [self jsonFromData:data needsConversion:&needsConversion];
169+
} @catch (NSException *e) {
170+
SEGLog(@"Unable to convert data from plist object to json; Exception: %@, data: %@", e, data);
171+
172+
[self removeKey:key];
173+
result = nil;
174+
}
168175
}
169176
}
170177
return result;

AnalyticsTests/FileStorageTest.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ class FileStorageTest : XCTestCase {
114114
let dictOut = storage.dictionary(forKey: key)
115115
XCTAssertEqual(dictOut as? [String: String], dictIn)
116116
}
117+
118+
func testShouldRemoveDictionaryForInvalidPlistConversion() {
119+
let key = "invalid.plist"
120+
let dictIn: [String: Any] = [
121+
"timestamp": TimeInterval.nan // `.nan` fails JSONSerialization
122+
]
123+
124+
let url = storage.url(forKey: key)
125+
(dictIn as NSDictionary).write(to: url, atomically: true)
126+
let dictOut = storage.dictionary(forKey: key)
127+
XCTAssertNil(dictOut)
128+
XCTAssertNil(try? url.checkResourceIsReachable())
129+
}
117130

118131
func testShouldWorkWithCrypto() {
119132
let url = FileStorage.applicationSupportDirectoryURL()

0 commit comments

Comments
 (0)