From e70ba1fb11b45641b3a0c8f7224bd7f8ca7edcd8 Mon Sep 17 00:00:00 2001 From: PoltoraIvana Date: Tue, 26 Nov 2024 10:25:43 +0100 Subject: [PATCH 1/3] Check RLMObjectBase for null after resolving thread safe reference In some cases, resolveThreadSafeReference: can return nil. Usually it happens in highly-concurrent contexts. This can lead to a crash --- Realm/RLMObjectBase.mm | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Realm/RLMObjectBase.mm b/Realm/RLMObjectBase.mm index 0cc1b5a114..6e72c23c45 100644 --- a/Realm/RLMObjectBase.mm +++ b/Realm/RLMObjectBase.mm @@ -723,6 +723,13 @@ - (void)addNotificationBlock:(RLMObjectNotificationCallback)block return; } RLMObjectBase *obj = [_realm resolveThreadSafeReference:tsr]; + if (!obj) { + error = [NSError errorWithDomain:RLMAppErrorDomain + code:RLMErrorObjectNotFound + userInfo:nil]; + block(nil, nil, nil, nil, error); + return; + } _object = realm::Object(_realm->_realm, *obj->_info->objectSchema, obj->_row); _token = _object.add_notification_callback(ObjectChangeCallbackWrapper{block, obj}, From 0418d34fe089937ef197fa24677683943748dde0 Mon Sep 17 00:00:00 2001 From: PoltoraIvana Date: Tue, 26 Nov 2024 10:30:33 +0100 Subject: [PATCH 2/3] Add new case to RealmError --- Realm/RLMError.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Realm/RLMError.h b/Realm/RLMError.h index c006ea4b8e..9479de963a 100644 --- a/Realm/RLMError.h +++ b/Realm/RLMError.h @@ -199,6 +199,10 @@ typedef RLM_ERROR_ENUM(NSInteger, RLMError, RLMErrorDomain) { subscription or the server will reject the write. */ RLMErrorNoSubscriptionForWrite = 22, + / ** + Denotes an error that occurs when resolving thread safe reference returns a nil object. + */ + RLMSyncErrorPermissionDeniedError = 23 }; #pragma mark - RLMSyncError From 9286062617496ac2d8d84c030217ece4aaec58ad Mon Sep 17 00:00:00 2001 From: PoltoraIvana Date: Tue, 26 Nov 2024 10:31:13 +0100 Subject: [PATCH 3/3] Rename new case in RealmError --- Realm/RLMError.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Realm/RLMError.h b/Realm/RLMError.h index 9479de963a..636833c240 100644 --- a/Realm/RLMError.h +++ b/Realm/RLMError.h @@ -202,7 +202,7 @@ typedef RLM_ERROR_ENUM(NSInteger, RLMError, RLMErrorDomain) { / ** Denotes an error that occurs when resolving thread safe reference returns a nil object. */ - RLMSyncErrorPermissionDeniedError = 23 + RLMErrorObjectNotFound = 23 }; #pragma mark - RLMSyncError