From 26a7d9c6e8efe951c2217159a737dc5e963f24cd Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 21 Jul 2018 12:00:31 -0700 Subject: [PATCH 1/6] Add nil check before calling block in YapDatabaseSecondaryIndexTransaction --- .../SecondaryIndex/YapDatabaseSecondaryIndexTransaction.m | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/YapDatabase/Extensions/SecondaryIndex/YapDatabaseSecondaryIndexTransaction.m b/YapDatabase/Extensions/SecondaryIndex/YapDatabaseSecondaryIndexTransaction.m index 2c98215ab..8b8ea3c5e 100644 --- a/YapDatabase/Extensions/SecondaryIndex/YapDatabaseSecondaryIndexTransaction.m +++ b/YapDatabase/Extensions/SecondaryIndex/YapDatabaseSecondaryIndexTransaction.m @@ -1287,7 +1287,9 @@ - (BOOL)enumerateKeysAndObjectsMatchingQuery:(YapDatabaseQuery *)query id object = nil; [self->databaseTransaction getCollectionKey:&ck object:&object forRowid:rowid]; - block(ck.collection, ck.key, object, stop); + if (ck && object) { + block(ck.collection, ck.key, object, stop); + } }]; return result; From 990ba41dbb8f8ab7897841e0f6a0c9800a1858a5 Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 21 Jul 2018 12:04:39 -0700 Subject: [PATCH 2/6] Abort ViewTransaction if page is missing --- YapDatabase/Extensions/View/YapDatabaseViewTransaction.m | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/YapDatabase/Extensions/View/YapDatabaseViewTransaction.m b/YapDatabase/Extensions/View/YapDatabaseViewTransaction.m index b7ab5de6c..33c41209e 100644 --- a/YapDatabase/Extensions/View/YapDatabaseViewTransaction.m +++ b/YapDatabase/Extensions/View/YapDatabaseViewTransaction.m @@ -1308,6 +1308,13 @@ - (void)insertRowid:(int64_t)rowid collectionKey:(YapCollectionKey *)collectionK NSString *pageKey = pageMetadata->pageKey; YapDatabaseViewPage *page = [self pageForPageKey:pageKey]; + + NSAssert(page != nil, @"Missing page in group(%@)", group); + + if (!page) { + YDBLogError(@"Missing page in group(%@)! Error inserting key(%@) collection(%@) in group(%@) at index(%lu) with page(%@) pageOffset(%lu)", group); + return; + } YDBLogVerbose(@"Inserting key(%@) collection(%@) in group(%@) at index(%lu) with page(%@) pageOffset(%lu)", collectionKey.key, collectionKey.collection, group, From 4fa7c0ceb0ee5f64264a728b13b77bb98829a1b0 Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 21 Jul 2018 12:26:47 -0700 Subject: [PATCH 3/6] Add nil checks before calling YapDatabaseViewSortingWithObjectBlock --- .../AutoView/YapDatabaseAutoViewTransaction.m | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m b/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m index ea0540c29..44e742533 100644 --- a/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m +++ b/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m @@ -555,10 +555,12 @@ - (void)insertRowid:(int64_t)rowid [databaseTransaction getCollectionKey:&another object:&anotherObject forRowid:anotherRowid]; - - return sortingBlock(databaseTransaction, group, - collectionKey.collection, collectionKey.key, object, - another.collection, another.key, anotherObject); + + if (object && anotherObject) { + return sortingBlock(databaseTransaction, group, + collectionKey.collection, collectionKey.key, object, + another.collection, another.key, anotherObject); + } } else if (sorting->blockType == YapDatabaseBlockTypeWithMetadata) { @@ -588,9 +590,11 @@ - (void)insertRowid:(int64_t)rowid metadata:&anotherMetadata forRowid:anotherRowid]; - return sortingBlock(databaseTransaction, group, - collectionKey.collection, collectionKey.key, object, metadata, - another.collection, another.key, anotherObject, anotherMetadata); + if (object && anotherObject) { + return sortingBlock(databaseTransaction, group, + collectionKey.collection, collectionKey.key, object, metadata, + another.collection, another.key, anotherObject, anotherMetadata); + } } #pragma clang diagnostic pop From ea42a3b76bd3742d52b780ca9ac2c5b40cbeb10f Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 21 Jul 2018 12:31:57 -0700 Subject: [PATCH 4/6] Return NSOrderedAscending --- .../Extensions/AutoView/YapDatabaseAutoViewTransaction.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m b/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m index 44e742533..eef1ee905 100644 --- a/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m +++ b/YapDatabase/Extensions/AutoView/YapDatabaseAutoViewTransaction.m @@ -560,6 +560,8 @@ - (void)insertRowid:(int64_t)rowid return sortingBlock(databaseTransaction, group, collectionKey.collection, collectionKey.key, object, another.collection, another.key, anotherObject); + } else { + return NSOrderedAscending; } } else if (sorting->blockType == YapDatabaseBlockTypeWithMetadata) @@ -594,6 +596,8 @@ - (void)insertRowid:(int64_t)rowid return sortingBlock(databaseTransaction, group, collectionKey.collection, collectionKey.key, object, metadata, another.collection, another.key, anotherObject, anotherMetadata); + } else { + return NSOrderedAscending; } } From 3eb96c0cdbea3ee2c9caa3b262b453c2fd7aa03c Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 21 Jul 2018 12:39:58 -0700 Subject: [PATCH 5/6] Add another nil check --- .../RTreeIndex/YapDatabaseRTreeIndexTransaction.m | 8 ++++++-- YapDatabase/YapDatabaseTransaction.m | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/YapDatabase/Extensions/RTreeIndex/YapDatabaseRTreeIndexTransaction.m b/YapDatabase/Extensions/RTreeIndex/YapDatabaseRTreeIndexTransaction.m index 0788ee462..d3998e550 100644 --- a/YapDatabase/Extensions/RTreeIndex/YapDatabaseRTreeIndexTransaction.m +++ b/YapDatabase/Extensions/RTreeIndex/YapDatabaseRTreeIndexTransaction.m @@ -1158,7 +1158,9 @@ - (BOOL)enumerateKeysAndObjectsMatchingQuery:(YapDatabaseQuery *)query id object = nil; [self->databaseTransaction getCollectionKey:&ck object:&object forRowid:rowid]; - block(ck.collection, ck.key, object, stop); + if (ck && object) { + block(ck.collection, ck.key, object, stop); + } }]; return result; @@ -1178,7 +1180,9 @@ - (BOOL)enumerateRowsMatchingQuery:(YapDatabaseQuery *)query id metadata = nil; [self->databaseTransaction getCollectionKey:&ck object:&object metadata:&metadata forRowid:rowid]; - block(ck.collection, ck.key, object, metadata, stop); + if (ck && object) { + block(ck.collection, ck.key, object, metadata, stop); + } }]; return result; diff --git a/YapDatabase/YapDatabaseTransaction.m b/YapDatabase/YapDatabaseTransaction.m index db55f28f7..2a737b795 100644 --- a/YapDatabase/YapDatabaseTransaction.m +++ b/YapDatabase/YapDatabaseTransaction.m @@ -3077,7 +3077,9 @@ - (void)_enumerateKeysAndObjectsInCollections:(NSArray *)collections } } - block(rowid, collection, key, object, &stop); + if (object) { + block(rowid, collection, key, object, &stop); + } if (stop || mutation.isMutated) break; } From 51263d14572eaf80de14e5dc47b3dd517c5357af Mon Sep 17 00:00:00 2001 From: Chris Ballinger Date: Sat, 21 Jul 2018 12:44:36 -0700 Subject: [PATCH 6/6] More nil checks --- YapDatabase/YapDatabaseTransaction.m | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/YapDatabase/YapDatabaseTransaction.m b/YapDatabase/YapDatabaseTransaction.m index 2a737b795..fb65edcf5 100644 --- a/YapDatabase/YapDatabaseTransaction.m +++ b/YapDatabase/YapDatabaseTransaction.m @@ -2634,7 +2634,9 @@ - (void)enumerateRowsForKeys:(NSArray *)keys [connection->metadataCache setObject:[YapNull null] forKey:cacheKey]; } - block(keyIndex, object, metadata, &stop); + if (object) { + block(keyIndex, object, metadata, &stop); + } [keyIndexDict removeObjectForKey:key]; @@ -2963,7 +2965,9 @@ - (void)_enumerateKeysAndObjectsInCollection:(NSString *)collection } } - block(rowid, key, object, &stop); + if (object) { + block(rowid, key, object, &stop); + } if (stop || mutation.isMutated) break; } @@ -3195,7 +3199,9 @@ - (void)_enumerateKeysAndObjectsInAllCollectionsUsingBlock: } } - block(rowid, collection, key, object, &stop); + if (object) { + block(rowid, collection, key, object, &stop); + } if (stop || mutation.isMutated) break; } @@ -3731,7 +3737,9 @@ - (void)_enumerateRowsInCollection:(NSString *)collection } } - block(rowid, key, object, metadata, &stop); + if (object) { + block(rowid, key, object, metadata, &stop); + } if (stop || mutation.isMutated) break; } @@ -3884,7 +3892,9 @@ - (void)_enumerateRowsInCollections:(NSArray *)collections } } - block(rowid, collection, key, object, metadata, &stop); + if (object) { + block(rowid, collection, key, object, metadata, &stop); + } if (stop || mutation.isMutated) break; } @@ -4029,7 +4039,9 @@ - (void)_enumerateRowsInAllCollectionsUsingBlock: } } - block(rowid, collection, key, object, metadata, &stop); + if (object) { + block(rowid, collection, key, object, metadata, &stop); + } if (stop || mutation.isMutated) break; }