From 4784179e04e4c5f9fc6e28f19c35a30ba714255a Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Fri, 25 Feb 2022 23:22:16 +0100 Subject: [PATCH] [clang-tidy] Fix a clang-analyzer-nullability.NullablePassedToNonnull occurence in src/platform/Darwin/KeyValueStoreManagerImpl.mm (#15586) --- src/platform/Darwin/KeyValueStoreManagerImpl.mm | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/platform/Darwin/KeyValueStoreManagerImpl.mm b/src/platform/Darwin/KeyValueStoreManagerImpl.mm index 00519e0adacd60..289357dac0f40e 100644 --- a/src/platform/Darwin/KeyValueStoreManagerImpl.mm +++ b/src/platform/Darwin/KeyValueStoreManagerImpl.mm @@ -152,9 +152,11 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n ReturnErrorCodeIf(fileName[0] == '\0', CHIP_ERROR_INVALID_ARGUMENT); NSURL * url = nullptr; + NSString * filepath = [NSString stringWithUTF8String:fileName]; + ReturnErrorCodeIf(filepath == nil, CHIP_ERROR_INVALID_ARGUMENT); // relative paths are relative to Documents folder - if (fileName[0] != '/') { + if ([filepath hasPrefix:@"/"]) { NSURL * documentsDirectory = [NSFileManager.defaultManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil @@ -167,9 +169,9 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n ChipLogProgress( DeviceLayer, "Found user documents directory: %s", [[documentsDirectory absoluteString] UTF8String]); - url = [NSURL URLWithString:[NSString stringWithUTF8String:fileName] relativeToURL:documentsDirectory]; + url = [NSURL URLWithString:filepath relativeToURL:documentsDirectory]; } else { - url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:fileName]]; + url = [NSURL fileURLWithPath:filepath]; } ReturnErrorCodeIf(url == nullptr, CHIP_ERROR_NO_MEMORY); @@ -274,9 +276,12 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n NSData * data = [[NSData alloc] initWithBytes:value length:value_size]; - KeyValueItem * item = FindItemForKey([[NSString alloc] initWithUTF8String:key], nil); + NSString * itemKey = [[NSString alloc] initWithUTF8String:key]; + ReturnErrorCodeIf(itemKey == nil, CHIP_ERROR_INVALID_ARGUMENT); + + KeyValueItem * item = FindItemForKey(itemKey, nil); if (!item) { - item = [[KeyValueItem alloc] initWithContext:gContext key:[[NSString alloc] initWithUTF8String:key] value:data]; + item = [[KeyValueItem alloc] initWithContext:gContext key:itemKey value:data]; [gContext performBlockAndWait:^{ [gContext insertObject:item]; }];