Skip to content

Commit

Permalink
Fix CoreData violations in the Darwin KVS implementation (#16149)
Browse files Browse the repository at this point in the history
* Fix CoreData violations in the Darwin KVS implementation

* Fix shadow declaration
  • Loading branch information
sagar-apple authored Mar 11, 2022
1 parent 5a28865 commit 9cc0fb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
ReferencedContainer = "container:CHIPTool.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "-com.apple.CoreData.ConcurrencyDebug 1"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
21 changes: 14 additions & 7 deletions src/platform/Darwin/KeyValueStoreManagerImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,28 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n
return CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND;
}

__block NSData * itemValue = nil;
// can only access this object on the managed queue
[gContext performBlockAndWait:^{
itemValue = item.value;
}];

if (read_bytes_size != nullptr) {
*read_bytes_size = item.value.length;
*read_bytes_size = itemValue.length;
}

if (value != nullptr) {
memcpy(value, item.value.bytes, std::min<size_t>((item.value.length), value_size));
memcpy(value, itemValue.bytes, std::min<size_t>((itemValue.length), value_size));
#if CHIP_CONFIG_DARWIN_STORAGE_VERBOSE_LOGGING
fprintf(stderr, "GETTING VALUE FOR: '%s': ", key);
for (size_t i = 0; i < std::min<size_t>((item.value.length), value_size); ++i) {
for (size_t i = 0; i < std::min<size_t>((itemValue.length), value_size); ++i) {
fprintf(stderr, "%02x ", static_cast<uint8_t *>(value)[i]);
}
fprintf(stderr, "\n");
#endif
}

if (item.value.length > value_size) {
if (itemValue.length > value_size) {
return CHIP_ERROR_BUFFER_TOO_SMALL;
}

Expand Down Expand Up @@ -281,12 +287,13 @@ - (instancetype)initWithContext:(nonnull NSManagedObjectContext *)context key:(n

KeyValueItem * item = FindItemForKey(itemKey, nil);
if (!item) {
item = [[KeyValueItem alloc] initWithContext:gContext key:itemKey value:data];
[gContext performBlockAndWait:^{
[gContext insertObject:item];
[gContext insertObject:[[KeyValueItem alloc] initWithContext:gContext key:itemKey value:data]];
}];
} else {
item.value = data;
[gContext performBlockAndWait:^{
item.value = data;
}];
}

__block BOOL success = NO;
Expand Down

0 comments on commit 9cc0fb3

Please sign in to comment.