Skip to content

Commit

Permalink
Fix issue #194
Browse files Browse the repository at this point in the history
- Remove use of SQLITE_EXTRA_INIT and SQLITE_EXTRA_SHUTDOWN
- Add SQLite patch to call sqlite3mc_initialize directly from within SQLite's guarded initialization context to avoid race condition (as described in issue #194)
- Add SQLite patch to call sqlite3mc_shutdown directly from sqlite3_shutdown
  • Loading branch information
utelle committed Feb 27, 2025
1 parent 37b8061 commit 92449bb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 2 additions & 0 deletions scripts/patchsqlite3.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ sed 's/sqlite3_file_control\(.*SQLITE_FCNTL_PRAGMA\)/sqlite3mcFileControlPragma\
| sed '/^ if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;/a \ if( sqlite3mcPagerHasCodec(pPager) != 0 ) return 0;' \
| sed '/^ }else if( USEFETCH(pPager) ){/c \ }else if( USEFETCH(pPager) && sqlite3mcPagerHasCodec(pPager) == 0 ){' \
| sed '/^ if( rc!=SQLITE_OK ) memset(&mem0, 0, sizeof(mem0));/a \\n \/\* Initialize wrapper for memory management.\*\/\n if( rc==SQLITE_OK ) {\n sqlite3mcInitMemoryMethods();\n }\n' \
| sed '/^ sqlite3GlobalConfig.isInit = 1;/i \ \n int sqlite3mc_initialize(const char*);\n rc = sqlite3mc_initialize(0);\n' \
| sed '/^ sqlite3_os_end();/i \ \n void sqlite3mc_shutdown(void);\n sqlite3mc_shutdown();\n' \
| sed '/Lock the source database handle./i \ \/\* Check whether databases are compatible with backup \*\/\n if (!sqlite3mcIsBackupSupported(pSrcDb, zSrcDb, pDestDb, zDestDb)){\n sqlite3ErrorWithMsg(pDestDb, SQLITE_ERROR, \"backup is not supported with incompatible source and target databases\");\n return NULL;\n }\n' \
| sed '/nRes = sqlite3BtreeGetRequestedReserve(pMain)/a \\n \/\* A VACUUM cannot change the pagesize of an encrypted database. \*\/\n if( db->nextPagesize ){\n extern void sqlite3mcCodecGetKey(sqlite3*, int, void**, int*);\n int nKey;\n char *zKey;\n sqlite3mcCodecGetKey(db, iDb, (void**)&zKey, &nKey);\n if( nKey ) db->nextPagesize = 0;\n }' \
13 changes: 0 additions & 13 deletions src/sqlite3mc.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@
#endif
#endif

/*
** Define function for extra initialization and extra shutdown
**
** The extra initialization function registers an extension function
** which will be automatically executed for each new database connection.
**
** The extra shutdown function will be executed on the invocation of sqlite3_shutdown.
** All created multiple ciphers VFSs will be unregistered and destroyed.
*/

#define SQLITE_EXTRA_INIT sqlite3mc_initialize
#define SQLITE_EXTRA_SHUTDOWN sqlite3mc_shutdown

/*
** Declare all internal functions as 'static' unless told otherwise
*/
Expand Down
8 changes: 8 additions & 0 deletions src/sqlite3patched.c
Original file line number Diff line number Diff line change
Expand Up @@ -181886,6 +181886,10 @@ SQLITE_API int sqlite3_initialize(void){
sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
sqlite3MemoryBarrier();

int sqlite3mc_initialize(const char*);
rc = sqlite3mc_initialize(0);

sqlite3GlobalConfig.isInit = 1;
#ifdef SQLITE_EXTRA_INIT
bRunExtraInit = 1;
Expand Down Expand Up @@ -181959,6 +181963,10 @@ SQLITE_API int sqlite3_shutdown(void){
void SQLITE_EXTRA_SHUTDOWN(void);
SQLITE_EXTRA_SHUTDOWN();
#endif

void sqlite3mc_shutdown(void);
sqlite3mc_shutdown();

sqlite3_os_end();
sqlite3_reset_auto_extension();
sqlite3GlobalConfig.isInit = 0;
Expand Down

0 comments on commit 92449bb

Please sign in to comment.