Skip to content

Commit 92449bb

Browse files
committed
Fix issue #194
- 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
1 parent 37b8061 commit 92449bb

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

scripts/patchsqlite3.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ sed 's/sqlite3_file_control\(.*SQLITE_FCNTL_PRAGMA\)/sqlite3mcFileControlPragma\
2727
| sed '/^ if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0;/a \ if( sqlite3mcPagerHasCodec(pPager) != 0 ) return 0;' \
2828
| sed '/^ }else if( USEFETCH(pPager) ){/c \ }else if( USEFETCH(pPager) && sqlite3mcPagerHasCodec(pPager) == 0 ){' \
2929
| 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' \
30+
| sed '/^ sqlite3GlobalConfig.isInit = 1;/i \ \n int sqlite3mc_initialize(const char*);\n rc = sqlite3mc_initialize(0);\n' \
31+
| sed '/^ sqlite3_os_end();/i \ \n void sqlite3mc_shutdown(void);\n sqlite3mc_shutdown();\n' \
3032
| 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' \
3133
| 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 }' \

src/sqlite3mc.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,6 @@
3737
#endif
3838
#endif
3939

40-
/*
41-
** Define function for extra initialization and extra shutdown
42-
**
43-
** The extra initialization function registers an extension function
44-
** which will be automatically executed for each new database connection.
45-
**
46-
** The extra shutdown function will be executed on the invocation of sqlite3_shutdown.
47-
** All created multiple ciphers VFSs will be unregistered and destroyed.
48-
*/
49-
50-
#define SQLITE_EXTRA_INIT sqlite3mc_initialize
51-
#define SQLITE_EXTRA_SHUTDOWN sqlite3mc_shutdown
52-
5340
/*
5441
** Declare all internal functions as 'static' unless told otherwise
5542
*/

src/sqlite3patched.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181886,6 +181886,10 @@ SQLITE_API int sqlite3_initialize(void){
181886181886
sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
181887181887
sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
181888181888
sqlite3MemoryBarrier();
181889+
181890+
int sqlite3mc_initialize(const char*);
181891+
rc = sqlite3mc_initialize(0);
181892+
181889181893
sqlite3GlobalConfig.isInit = 1;
181890181894
#ifdef SQLITE_EXTRA_INIT
181891181895
bRunExtraInit = 1;
@@ -181959,6 +181963,10 @@ SQLITE_API int sqlite3_shutdown(void){
181959181963
void SQLITE_EXTRA_SHUTDOWN(void);
181960181964
SQLITE_EXTRA_SHUTDOWN();
181961181965
#endif
181966+
181967+
void sqlite3mc_shutdown(void);
181968+
sqlite3mc_shutdown();
181969+
181962181970
sqlite3_os_end();
181963181971
sqlite3_reset_auto_extension();
181964181972
sqlite3GlobalConfig.isInit = 0;

0 commit comments

Comments
 (0)