我正在尝试为大型数据库转换准备几个SQLite语句。
#define HAM_BUFFER_SIZE 4096
typedef struct ham_fcc_sqlite {
sqlite3 *database;
char *sql_errmsg;
sqlite3_stmt *am_stmt;
sqlite3_stmt *en_stmt;
sqlite3_stmt *hd_stmt;
sqlite3_stmt *hs_stmt;
int include_optional;
unsigned int sql_insert_calls;
} ham_fcc_sqlite;
int ham_sqlite_init(ham_fcc_sqlite **fcc_sqlite, const int include_optional) {
(*fcc_sqlite) = malloc(sizeof(ham_fcc_sqlite));
if((*fcc_sqlite) == NULL)
return HAM_ERROR_SQLITE_INIT;
if(ham_sqlite_open_database_connection(&(*fcc_sqlite)->database, HAM_SQLITE_FILE_NAME)) {
free((*fcc_sqlite));
(*fcc_sqlite) = NULL;
return HAM_ERROR_SQLITE_OPEN_DATABASE_CONNECTION;
}
(*fcc_sqlite)->include_optional = include_optional;
(*fcc_sqlite)->sql_insert_calls = 0;
return HAM_OK;
}
int ham_sqlite_sql_prepare_stmt(ham_fcc_sqlite *fcc_sqlite) {
if(sqlite3_prepare_v2(fcc_sqlite->database, HAM_SQLITE_INSERT_FCC_AM, HAM_BUFFER_SIZE,
&fcc_sqlite->am_stmt, NULL))
return HAM_ERROR_SQLITE_PREPARE_STMT;
if(sqlite3_prepare_v2(fcc_sqlite->database, HAM_SQLITE_INSERT_FCC_EN, HAM_BUFFER_SIZE,
&fcc_sqlite->en_stmt, NULL))
return HAM_ERROR_SQLITE_PREPARE_STMT;
if(sqlite3_prepare_v2(fcc_sqlite->database, HAM_SQLITE_INSERT_FCC_HD, HAM_BUFFER_SIZE,
&fcc_sqlite->hd_stmt, NULL))
return HAM_ERROR_SQLITE_PREPARE_STMT;
if(sqlite3_prepare_v2(fcc_sqlite->database, HAM_SQLITE_INSERT_FCC_HS, HAM_BUFFER_SIZE,
&fcc_sqlite->hs_stmt, NULL))
return HAM_ERROR_SQLITE_PREPARE_STMT;
return HAM_OK;
}
此代码在Windows上运行良好,在使用-O3时在linux上运行。在没有优化的情况下,在第四个准备调用上发生分段错误。这是在向库中添加新的无关函数后开始出现的。此外,如果我将其静态编译到控制程序中,而不是动态链接,则不会发生段错误。关于可能导致这种情况的任何想法?
编辑:
这是调用堆栈。
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff77847d7 in sqlite3Prepare (db=0x605aa8,
zSql=0x7ffff79d41c8 "INSERT INTO fcc_hs VALUES (@record_type,@unique_system_identifier,@uls_file_number,@callsign,@log_date,@code)", nBytes=4096, saveSqlFlag=1,
pReprepare=0x0, ppStmt=0x605a08, pzTail=0x0) at /home/aristotle/devel/libhamdata/sqlite3.c:112577
112577 if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
#0 0x00007ffff77847d7 in sqlite3Prepare (db=0x605aa8,
zSql=0x7ffff79d41c8 "INSERT INTO fcc_hs VALUES (@record_type,@unique_system_identifier,@uls_file_number,@callsign,@log_date,@code)", nBytes=4096, saveSqlFlag=1,
pReprepare=0x0, ppStmt=0x605a08, pzTail=0x0) at /home/aristotle/devel/libhamdata/sqlite3.c:112577
#1 0x00007ffff7784c0a in sqlite3LockAndPrepare (db=0x605aa8,
zSql=0x7ffff79d41c8 "INSERT INTO fcc_hs VALUES (@record_type,@unique_system_identifier,@uls_file_number,@callsign,@log_date,@code)", nBytes=4096, saveSqlFlag=1, pOld=0x0,
ppStmt=0x605a08, pzTail=0x0) at /home/aristotle/devel/libhamdata/sqlite3.c:112688
#2 0x00007ffff7784dd6 in sqlite3_prepare_v2 (db=0x605aa8,
zSql=0x7ffff79d41c8 "INSERT INTO fcc_hs VALUES (@record_type,@unique_system_identifier,@uls_file_number,@callsign,@log_date,@code)", nBytes=4096, ppStmt=0x605a08, pzTail=0x0)
at /home/aristotle/devel/libhamdata/sqlite3.c:112764
#3 0x00007ffff79d258e in ham_sqlite_sql_prepare_stmt (fcc_sqlite=0x6059e0) at /home/aristotle/devel/libhamdata/libhamdata.c:797
#4 0x00007ffff79d21ff in ham_fcc_to_sqlite (fcc_database=0x601010) at /home/aristotle/devel/libhamdata/libhamdata.c:721
#5 0x0000000000400804 in main (argc=1, argv=0x7fffffffe6e8) at /home/aristotle/devel/libhamdata/ham_data.c:37
编辑2:
每个请求的sqlite3Prepare代码。
static int sqlite3Prepare(
sqlite3 *db, /* Database handle. */
const char *zSql, /* UTF-8 encoded SQL statement. */
int nBytes, /* Length of zSql in bytes. */
int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */
Vdbe *pReprepare, /* VM being reprepared */
sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */
const char **pzTail /* OUT: End of parsed string */
){
Parse *pParse; /* Parsing context */
char *zErrMsg = 0; /* Error message */
int rc = SQLITE_OK; /* Result code */
int i; /* Loop counter */
/* Allocate the parsing context */
pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
if( pParse==0 ){
rc = SQLITE_NOMEM_BKPT;
goto end_prepare;
}
pParse->pReprepare = pReprepare;
assert( ppStmt && *ppStmt==0 );
/* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
assert( sqlite3_mutex_held(db->mutex) );
/* Check to verify that it is possible to get a read lock on all
** database schemas. The inability to get a read lock indicates that
** some other database connection is holding a write-lock, which in
** turn means that the other connection has made uncommitted changes
** to the schema.
**
** Were we to proceed and prepare the statement against the uncommitted
** schema changes and if those schema changes are subsequently rolled
** back and different changes are made in their place, then when this
** prepared statement goes to run the schema cookie would fail to detect
** the schema change. Disaster would follow.
**
** This thread is currently holding mutexes on all Btrees (because
** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
** is not possible for another thread to start a new schema change
** while this routine is running. Hence, we do not need to hold
** locks on the schema, we just need to make sure nobody else is
** holding them.
**
** Note that setting READ_UNCOMMITTED overrides most lock detection,
** but it does *not* override schema lock detection, so this all still
** works even if READ_UNCOMMITTED is set.
*/
for(i=0; i<db->nDb; i++) {
Btree *pBt = db->aDb[i].pBt;
if( pBt ){
assert( sqlite3BtreeHoldsMutex(pBt) );
rc = sqlite3BtreeSchemaLocked(pBt);
if( rc ){
const char *zDb = db->aDb[i].zName;
sqlite3ErrorWithMsg(db, rc, "database schema is locked: %s", zDb);
testcase( db->flags & SQLITE_ReadUncommitted );
goto end_prepare;
}
}
}
sqlite3VtabUnlockList(db);
pParse->db = db;
pParse->nQueryLoop = 0; /* Logarithmic, so 0 really means 1 */
if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
char *zSqlCopy;
int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
testcase( nBytes==mxLen );
testcase( nBytes==mxLen+1 );
if( nBytes>mxLen ){
sqlite3ErrorWithMsg(db, SQLITE_TOOBIG, "statement too long");
rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
goto end_prepare;
}
zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
if( zSqlCopy ){
sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
sqlite3DbFree(db, zSqlCopy);
}else{
pParse->zTail = &zSql[nBytes];
}
}else{
sqlite3RunParser(pParse, zSql, &zErrMsg);
}
assert( 0==pParse->nQueryLoop );
if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
if( pParse->checkSchema ){
schemaIsValid(pParse);
}
if( db->mallocFailed ){
pParse->rc = SQLITE_NOMEM_BKPT;
}
if( pzTail ){
*pzTail = pParse->zTail;
}
rc = pParse->rc;
#ifndef SQLITE_OMIT_EXPLAIN
if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
static const char * const azColName[] = {
"addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
"selectid", "order", "from", "detail"
};
int iFirst, mx;
if( pParse->explain==2 ){
sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
iFirst = 8;
mx = 12;
}else{
sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
iFirst = 0;
mx = 8;
}
for(i=iFirst; i<mx; i++){
sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
azColName[i], SQLITE_STATIC);
}
}
#endif
if( db->init.busy==0 ){
Vdbe *pVdbe = pParse->pVdbe;
sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
}
if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
sqlite3VdbeFinalize(pParse->pVdbe);
assert(!(*ppStmt));
}else{
*ppStmt = (sqlite3_stmt*)pParse->pVdbe;
}
if( zErrMsg ){
sqlite3ErrorWithMsg(db, rc, "%s", zErrMsg);
sqlite3DbFree(db, zErrMsg);
}else{
sqlite3Error(db, rc);
}
/* Delete any TriggerPrg structures allocated while parsing this statement. */
while( pParse->pTriggerPrg ){
TriggerPrg *pT = pParse->pTriggerPrg;
pParse->pTriggerPrg = pT->pNext;
sqlite3DbFree(db, pT);
}
end_prepare:
sqlite3ParserReset(pParse);
sqlite3StackFree(db, pParse);
rc = sqlite3ApiExit(db, rc);
assert( (rc&db->errMask)==rc );
return rc;
}
答案 0 :(得分:1)
所以,经过几个小时的拔毛,我发现崩溃发生在这句话中:
if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
访问zSql[nBytes-1]
时会发生这种情况。 zSql
是要准备的SQL语句。 nBytes是zSql
可以的最大字节数。由于命令远不及我尝试访问该索引处的内存的最大长度(4096),因此发生访问冲突。
要解决此问题,我将最大SQL长度更改为-1。这导致SQLite使用zSql
一个空终止符 - 这就是我们所需要的。
对于那些有兴趣了解整个图书馆的人,可以找到它here
感谢@theunamedguy的帮助。