@@ -152,9 +152,11 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n
152
152
ReturnErrorCodeIf (fileName[0 ] == ' \0 ' , CHIP_ERROR_INVALID_ARGUMENT);
153
153
154
154
NSURL * url = nullptr ;
155
+ NSString * filepath = [NSString stringWithUTF8String: fileName];
156
+ ReturnErrorCodeIf (filepath == nil , CHIP_ERROR_INVALID_ARGUMENT);
155
157
156
158
// relative paths are relative to Documents folder
157
- if (fileName[ 0 ] != ' / ' ) {
159
+ if ([filepath hasPrefix: @" / " ] ) {
158
160
NSURL * documentsDirectory = [NSFileManager .defaultManager URLForDirectory: NSDocumentDirectory
159
161
inDomain: NSUserDomainMask
160
162
appropriateForURL: nil
@@ -167,9 +169,9 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n
167
169
ChipLogProgress (
168
170
DeviceLayer, " Found user documents directory: %s" , [[documentsDirectory absoluteString ] UTF8String ]);
169
171
170
- url = [NSURL URLWithString: [ NSString stringWithUTF8String: fileName] relativeToURL: documentsDirectory];
172
+ url = [NSURL URLWithString: filepath relativeToURL: documentsDirectory];
171
173
} else {
172
- url = [NSURL fileURLWithPath: [ NSString stringWithUTF8String: fileName] ];
174
+ url = [NSURL fileURLWithPath: filepath ];
173
175
}
174
176
ReturnErrorCodeIf (url == nullptr , CHIP_ERROR_NO_MEMORY);
175
177
@@ -274,9 +276,12 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n
274
276
275
277
NSData * data = [[NSData alloc ] initWithBytes: value length: value_size];
276
278
277
- KeyValueItem * item = FindItemForKey ([[NSString alloc ] initWithUTF8String: key], nil );
279
+ NSString * itemKey = [[NSString alloc ] initWithUTF8String: key];
280
+ ReturnErrorCodeIf (itemKey == nil , CHIP_ERROR_INVALID_ARGUMENT);
281
+
282
+ KeyValueItem * item = FindItemForKey (itemKey, nil );
278
283
if (!item) {
279
- item = [[KeyValueItem alloc ] initWithContext: gContext key: [[ NSString alloc ] initWithUTF8String: key] value: data];
284
+ item = [[KeyValueItem alloc ] initWithContext: gContext key: itemKey value: data];
280
285
[gContext performBlockAndWait: ^{
281
286
[gContext insertObject: item];
282
287
}];
0 commit comments