Skip to content

Commit 9ce3779

Browse files
authored
Fix issues around plist->json conversion & nil values (#862)
* Fixing a crash from plist->json conversion where result is not actually a dictionary. * Made code flow the same as non-conversion. * Set compatibility to 10.0. * Fixed issues around setting nil values even though they are expected.
1 parent 07515b3 commit 9ce3779

File tree

4 files changed

+54
-31
lines changed

4 files changed

+54
-31
lines changed

Analytics.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,7 @@
866866
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
867867
GCC_WARN_UNUSED_FUNCTION = YES;
868868
GCC_WARN_UNUSED_VARIABLE = YES;
869-
IPHONEOS_DEPLOYMENT_TARGET = 10.1;
869+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
870870
MTL_ENABLE_DEBUG_INFO = YES;
871871
ONLY_ACTIVE_ARCH = YES;
872872
SDKROOT = iphoneos;
@@ -919,7 +919,7 @@
919919
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
920920
GCC_WARN_UNUSED_FUNCTION = YES;
921921
GCC_WARN_UNUSED_VARIABLE = YES;
922-
IPHONEOS_DEPLOYMENT_TARGET = 10.1;
922+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
923923
MTL_ENABLE_DEBUG_INFO = NO;
924924
SDKROOT = iphoneos;
925925
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

Analytics/Classes/Internal/SEGFileStorage.m

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ - (void)resetAll
5151
- (void)setData:(NSData *)data forKey:(NSString *)key
5252
{
5353
NSURL *url = [self urlForKey:key];
54+
55+
// a nil value was supplied, remove the storage for said key.
56+
if (data == nil) {
57+
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
58+
return;
59+
}
60+
5461
if (self.crypto) {
5562
NSData *encryptedData = [self.crypto encrypt:data];
5663
[encryptedData writeToURL:url atomically:YES];
@@ -80,40 +87,38 @@ - (NSData *)dataForKey:(NSString *)key
8087
return data;
8188
}
8289

83-
- (NSDictionary *)dictionaryForKey:(NSString *)key
90+
- (nullable NSDictionary *)dictionaryForKey:(NSString *)key
8491
{
8592
return [self jsonForKey:key];
8693
}
8794

88-
- (void)setDictionary:(NSDictionary *)dictionary forKey:(NSString *)key
95+
- (void)setDictionary:(nullable NSDictionary *)dictionary forKey:(NSString *)key
8996
{
9097
[self setJSON:dictionary forKey:key];
9198
}
9299

93-
- (NSArray *)arrayForKey:(NSString *)key
100+
- (nullable NSArray *)arrayForKey:(NSString *)key
94101
{
95102
return [self jsonForKey:key];
96103
}
97104

98-
- (void)setArray:(NSArray *)array forKey:(NSString *)key
105+
- (void)setArray:(nullable NSArray *)array forKey:(NSString *)key
99106
{
100107
[self setJSON:array forKey:key];
101108
}
102109

103-
- (NSString *)stringForKey:(NSString *)key
110+
- (nullable NSString *)stringForKey:(NSString *)key
104111
{
105-
// unlike plists, we can't have stray values, they need to be
106-
// in a dictionary or array container.
107112
NSDictionary *data = [self jsonForKey:key];
108-
return data[key];
113+
if (data) {
114+
return data[key];
115+
}
116+
return nil;
109117
}
110118

111-
- (void)setString:(NSString *)string forKey:(NSString *)key
119+
- (void)setString:(nullable NSString *)string forKey:(NSString *)key
112120
{
113-
// unlike plists, we can't have stray values, they need to be
114-
// in a dictionary or array container.
115-
NSDictionary *data = @{key: string};
116-
[self setJSON:data forKey:key];
121+
[self setJSON:string forKey:key];
117122
}
118123

119124

@@ -148,31 +153,39 @@ - (id _Nullable)jsonForKey:(NSString *)key
148153
result = [self jsonFromData:data needsConversion:&needsConversion];
149154
if (needsConversion) {
150155
[self setJSON:result forKey:key];
156+
// maybe a little repetitive, but we want to recreate the same path it would
157+
// take if it weren't being converted.
158+
data = [self dataForKey:key];
159+
result = [self jsonFromData:data needsConversion:&needsConversion];
151160
}
152161
}
153162
return result;
154163
}
155164

156-
- (void)setJSON:(id _Nonnull)plist forKey:(NSString *)key
165+
- (void)setJSON:(id _Nonnull)json forKey:(NSString *)key
157166
{
158167
NSDictionary *dict = nil;
159168

160169
// json doesn't allow stand alone values like plist (previous storage format) does so
161170
// we need to massage it a little.
162-
if ([plist isKindOfClass:[NSDictionary class]] || [plist isKindOfClass:[NSArray class]]) {
163-
dict = plist;
164-
} else {
165-
dict = @{key: plist};
171+
if (json) {
172+
if ([json isKindOfClass:[NSDictionary class]] || [json isKindOfClass:[NSArray class]]) {
173+
dict = json;
174+
} else {
175+
dict = @{key: json};
176+
}
166177
}
167178

168179
NSData *data = [self dataFromJSON:dict];
169-
if (data) {
170-
[self setData:data forKey:key];
171-
}
180+
[self setData:data forKey:key];
172181
}
173182

174183
- (NSData *_Nullable)dataFromJSON:(id)json
175184
{
185+
if (json == nil) {
186+
return nil;
187+
}
188+
176189
NSError *error = nil;
177190
NSData *data = [NSJSONSerialization dataWithJSONObject:json options:0 error:&error];
178191
if (error) {

Analytics/Classes/Internal/SEGStorage.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
- (void)removeKey:(NSString *_Nonnull)key;
1616
- (void)resetAll;
1717

18-
- (void)setData:(NSData *_Nonnull)data forKey:(NSString *_Nonnull)key;
18+
- (void)setData:(NSData *_Nullable)data forKey:(NSString *_Nonnull)key;
1919
- (NSData *_Nullable)dataForKey:(NSString *_Nonnull)key;
2020

21-
- (void)setDictionary:(NSDictionary *_Nonnull)dictionary forKey:(NSString *_Nonnull)key;
21+
- (void)setDictionary:(NSDictionary *_Nullable)dictionary forKey:(NSString *_Nonnull)key;
2222
- (NSDictionary *_Nullable)dictionaryForKey:(NSString *_Nonnull)key;
2323

24-
- (void)setArray:(NSArray *_Nonnull)array forKey:(NSString *_Nonnull)key;
24+
- (void)setArray:(NSArray *_Nullable)array forKey:(NSString *_Nonnull)key;
2525
- (NSArray *_Nullable)arrayForKey:(NSString *_Nonnull)key;
2626

27-
- (void)setString:(NSString *_Nonnull)string forKey:(NSString *_Nonnull)key;
27+
- (void)setString:(NSString *_Nullable)string forKey:(NSString *_Nonnull)key;
2828
- (NSString *_Nullable)stringForKey:(NSString *_Nonnull)key;
2929

3030
// Number and Booleans are intentionally omitted at the moment because they are not needed

Analytics/Classes/Internal/SEGUserDefaultsStorage.m

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ - (void)resetAll
4646
[self.defaults synchronize];
4747
}
4848

49+
- (void)setObject:(id)object forKey:(NSString *)key
50+
{
51+
// pass through for NSUserDefaults to remove keys if supplied a nil value.
52+
if (object) {
53+
[self.defaults setObject:object forKey:key];
54+
} else {
55+
[self.defaults removeObjectForKey:key];
56+
}
57+
}
58+
4959
- (void)setData:(NSData *)data forKey:(NSString *)key
5060
{
5161
key = [self namespacedKey:key];
@@ -54,7 +64,7 @@ - (void)setData:(NSData *)data forKey:(NSString *)key
5464
return;
5565
}
5666
NSData *encryptedData = [self.crypto encrypt:data];
57-
[self.defaults setObject:encryptedData forKey:key];
67+
[self setObject:encryptedData forKey:key];
5868
}
5969

6070
- (NSData *)dataForKey:(NSString *)key
@@ -84,7 +94,7 @@ - (void)setDictionary:(NSDictionary *)dictionary forKey:(NSString *)key
8494
{
8595
if (!self.crypto) {
8696
key = [self namespacedKey:key];
87-
[self.defaults setObject:dictionary forKey:key];
97+
[self setObject:dictionary forKey:key];
8898
return;
8999
}
90100
[self setPlist:dictionary forKey:key];
@@ -103,7 +113,7 @@ - (void)setArray:(NSArray *)array forKey:(NSString *)key
103113
{
104114
if (!self.crypto) {
105115
key = [self namespacedKey:key];
106-
[self.defaults setObject:array forKey:key];
116+
[self setObject:array forKey:key];
107117
return;
108118
}
109119
[self setPlist:array forKey:key];
@@ -122,7 +132,7 @@ - (void)setString:(NSString *)string forKey:(NSString *)key
122132
{
123133
if (!self.crypto) {
124134
key = [self namespacedKey:key];
125-
[self.defaults setObject:string forKey:key];
135+
[self setObject:string forKey:key];
126136
return;
127137
}
128138
[self setPlist:string forKey:key];

0 commit comments

Comments
 (0)