c - 代码未优化时的段错误——SQLite

标签 c sqlite segmentation-fault

我正在尝试为大型数据库转换准备几个 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:

sqlite3根据请求准备代码。

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;
}

最佳答案

所以,经过几个小时的拉扯,我发现崩溃发生在这个语句中:

if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){

访问 zSql[nBytes-1] 时会发生这种情况。 zSql 是要准备的SQL语句。 nBytes 是 zSql 的最大字节数。由于该命令远不及我给出的尝试访问该索引处内存的最大长度 (4096),因此会发生访问冲突。

为了解决这个问题,我将最大 SQL 长度更改为 -1。这导致 SQLite 使用 zSql 直到空终止符——这就是我们所需要的。

有兴趣看完整个图书馆的可以找here

感谢@theunamedguy 的帮助。

关于c - 代码未优化时的段错误——SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38211886/

相关文章:

c - 如何使用指针访问结构中列表中的结构?

c - 从队列中删除头元素

python - SQLAlchemy db.create_all() 错误,未创建数据库

sqlite - 使用 swift + FMDB 插入 NULL

c - 为什么 memcpy 在第二次尝试时不起作用?

c - OpenSSL C 多线程客户端段错误

c++ - Vector::erase 段错误

检查命令行参数是否为负数

c - 为什么 char [] 作为静态

android - 将 SQLite 数据库同步到 App Engine 数据存储区?