Skip to content

Commit

Permalink
OGR2SQLITE_Setup(): robustify against potential crashing scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed Jan 13, 2024
1 parent 4daa319 commit 8b5f7c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ogr/ogrsf_frmts/sqlite/ogrsqliteexecutesql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,13 @@ OGRLayer *OGRSQLiteExecuteSQL(GDALDataset *poDS, const char *pszStatement,
/* Attach the Virtual Table OGR2SQLITE module to it. */
/* -------------------------------------------------------------------- */
OGR2SQLITEModule *poModule = OGR2SQLITE_Setup(poDS, poSQLiteDS);
if (!poModule)
{
delete poSQLiteDS;
VSIUnlink(pszTmpDBName);
CPLFree(pszTmpDBName);
return nullptr;
}
sqlite3 *hDB = poSQLiteDS->GetDB();

/* -------------------------------------------------------------------- */
Expand Down
11 changes: 11 additions & 0 deletions ogr/ogrsf_frmts/sqlite/ogrsqlitevirtualogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2721,6 +2721,17 @@ int OGR2SQLITEModule::Setup(sqlite3 *hDBIn)
OGR2SQLITEModule *OGR2SQLITE_Setup(GDALDataset *poDS,
OGRSQLiteDataSource *poSQLiteDS)
{
if (sqlite3_api == nullptr)
{
// Unlikely to happen. One theoretical possibility would be that:
// - thread A calls OGR2SQLITE_Register(), which calls sqlite3_auto_extension((void (*)(void))OGR2SQLITE_static_register)
// - thread B calls sqlite3_reset_auto_extension()
// - thread A opens a sqlite3 handle (which normally would have caused OGR2SQLITE_static_register() to be called, and setting the sqlite3_api static variable, without prior B intervention.
// - thread A calls us (OGR2SQLITE_Setup()) with sqlite3_api still set to its initial nullptr value
CPLError(CE_Failure, CPLE_AppDefined,
"OGR2SQLITE_Setup() failed due to sqlite3_api == nullptr");
return nullptr;
}
OGR2SQLITEModule *poModule = new OGR2SQLITEModule();
poModule->Setup(poDS, poSQLiteDS);
return poModule;
Expand Down

0 comments on commit 8b5f7c7

Please sign in to comment.