Skip to content

Commit

Permalink
[clang-tidy] Fix a clang-analyzer-nullability.NullablePassedToNonnull…
Browse files Browse the repository at this point in the history
… occurence in src/platform/Darwin/KeyValueStoreManagerImpl.mm (#15586)
  • Loading branch information
vivien-apple authored Feb 25, 2022
1 parent 630c6c5 commit ad6d418
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/platform/Darwin/KeyValueStoreManagerImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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];
}];
Expand Down

2 comments on commit ad6d418

@danh-geo
Copy link
Contributor

@danh-geo danh-geo commented on ad6d418 Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @vivien-apple ,
I'm building the HEAD of master for the iOS CHIP Tool and I notice that when I tap the "QRCode scanner" on the first screen, the app crashes.
I've managed to pinpoint the crash to these changes / this commit.
If I use the HEAD of master, but keep src/platform/Darwin/KeyValueStoreManagerImpl.mm as it was before the changes above, I can see that it no longer crashes.

I think just reverting this line if ([filepath hasPrefix:@"/"]) { to if (fileName[0] != '/') { should fix it.
From what I can see, your change makes it hit the else block of code which causes the crash. If it hits the if block then its OK.

The app crashes at this point to l.234 of CHIPDeviceController.mm of errorCode = factory.Init(params);

Hope this helps.
Thanks

@vivien-apple
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danh-geo
I did a stupid mistake while updating this code. I assume replacing if ([filepath hasPrefix:@"/"]) by if (![filepath hasPrefix:@"/"]) will work. I have reverted the if/else case :(

Please sign in to comment.