Skip to content

Commit c8ce6f0

Browse files
committed
Removes use of static mutex in sqlcipher_extra_shutdown()
The sqlcipher_extra_shutdown() function is called by sqlite3_shutdown() to cleanup internal SQLCipher resources. However, many applications do not call sqlite3_shutdown(). In order to ensure that SQLCipher resources are cleaned up even if sqlite3_shutdown() is omitted, sqlcipher_extra_shutdown() is also called from atexit and from a library finalizer (fini/DllMain). Previously, sqlcipher_extra_shutdown() internally locked a global static mutex prior to clean up. However, this introduced an edge case where, if the library was compiled with SQLITE_OMIT_AUTOINIT, and sqlite_shutdown() was called explicitly, a subsequent call to sqlcipher_extra_shutdown() from atexit or the finalizer could reallocate a new mutex that would never be freed. This change removes the use of the mutex from sqlcipher_extra_shutdown() entirely. The SQLite documentation makes it clear that sqlite3_shutdown() is NOT threadsafe (https://www.sqlite.org/c3ref/initialize.html) so an application must already guarantee that is called from a single thread. Other invocations of sqlcipher_extra_shutdown will also be called in a single-threaded context. As a result sqlcipher_extra_shutdown() should not need to make use of a mutext internally, and its removal solves the previous edge case problem.
1 parent 7e14e79 commit c8ce6f0

1 file changed

Lines changed: 0 additions & 7 deletions

File tree

src/sqlcipher.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,6 @@ int sqlcipher_extra_init(const char* arg) {
589589
void sqlcipher_extra_shutdown(void) {
590590
int i = 0;
591591
sqlcipher_provider *provider = NULL;
592-
sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
593-
if(mutex) {
594-
sqlite3_mutex_enter(mutex);
595-
}
596592

597593
/* if sqlcipher hasn't been initialized or the shutdown already completed exit early */
598594
if(!sqlcipher_init || sqlcipher_shutdown) {
@@ -663,9 +659,6 @@ void sqlcipher_extra_shutdown(void) {
663659
sqlcipher_init = 0;
664660
sqlcipher_init_error = SQLITE_ERROR;
665661
sqlcipher_shutdown = 1;
666-
if(mutex) {
667-
sqlite3_mutex_leave(mutex);
668-
}
669662
}
670663

671664
static void sqlcipher_shield(unsigned char *in, int sz) {

0 commit comments

Comments
 (0)