couchbase / couchbase-lite-ios

Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.
Apache License 2.0
1.62k stars 297 forks source link

CBL-5659 : Fix a released query context may be used in observer callback #3275

Closed pasin closed 4 months ago

pasin commented 4 months ago

Implemented CBLContextManager class for retaining and mapping the object with its pointer value which can be used as the context for LiteCore's callbacks (e.g. use when creating c4queryobserver objects). The implementation simply stores the object in a map by using its memory address as the key and returns the memory address as the pointer value.

Updated CBLQueryObserver to use CBLContextManager to register its query context and unregister the context when the C4QueryObserver is disabled. The C4QueryObserver's callback now can verified that the query context is still valid before using it. Also when using the query context, the query context is retained in the callback itself.

Updated CBLQueryObserver to get the C4QueryEnumerator object inside the callback as the enumerator object to prevent using the already freed C4QueryObserver in the notification queue.

Added a test for CBSE-16662 which is related to this issue and two tests to check that there are no notification received without crash (CBL-5659) after removing the token.

Added an internal debug build only C4QueryObserverCallbackDelayInterval config for testing the fix.

Found that the retain cyble between CBLQueryObserver and CBLQuery and CBLListenerToken can be improved.

Updated LiteCore to 3.1.7-1 to get the fix for CBSE-16662.