/ Check-in [83d6416a]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merge latest trunk changes into this branch.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | exp-window-functions
Files: files | file ages | folders
SHA3-256: 83d6416a868fac81a78c9507185a48d00920e4322276245e285946f760915f4a
User & Date: dan 2018-06-04 08:28:18
Context
2018-06-04
18:55
Add implementation of window function ntile(). check-in: 3f093f60 user: dan tags: exp-window-functions
08:28
Merge latest trunk changes into this branch. check-in: 83d6416a user: dan tags: exp-window-functions
08:22
Add support for window function cume_dist(). Improve tests for percent_rank(). check-in: 76543f7d user: dan tags: exp-window-functions
2018-06-02
19:14
Avoid using a misaligned pointer. check-in: 1ecb3aa1 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to autoconf/Makefile.am.

    10     10   EXTRA_sqlite3_SOURCES = sqlite3.c
    11     11   sqlite3_LDADD = @EXTRA_SHELL_OBJ@ @READLINE_LIBS@
    12     12   sqlite3_DEPENDENCIES = @EXTRA_SHELL_OBJ@
    13     13   sqlite3_CFLAGS = $(AM_CFLAGS) -DSQLITE_ENABLE_EXPLAIN_COMMENTS -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_ENABLE_STMTVTAB -DSQLITE_ENABLE_DBSTAT_VTAB $(SHELL_CFLAGS)
    14     14   
    15     15   include_HEADERS = sqlite3.h sqlite3ext.h
    16     16   
    17         -EXTRA_DIST = sqlite3.1 tea Makefile.msc sqlite3.rc README.txt Replace.cs
           17  +EXTRA_DIST = sqlite3.1 tea Makefile.msc sqlite3.rc README.txt Replace.cs Makefile.fallback
    18     18   pkgconfigdir = ${libdir}/pkgconfig
    19     19   pkgconfig_DATA = sqlite3.pc
    20     20   
    21     21   man_MANS = sqlite3.1

Added autoconf/Makefile.fallback.

            1  +#!/usr/bin/make
            2  +#
            3  +# If the configure script does not work, then this Makefile is available
            4  +# as a backup.  Manually configure the variables below.
            5  +#
            6  +# Note:  This makefile works out-of-the-box on MacOS 10.2 (Jaguar)
            7  +#
            8  +CC = gcc
            9  +CFLAGS = -O0 -I.
           10  +LIBS = -lz
           11  +COPTS += -D_BSD_SOURCE
           12  +COPTS += -DSQLITE_ENABLE_LOCKING_STYLE=0
           13  +COPTS += -DSQLITE_THREADSAFE=0
           14  +COPTS += -DSQLITE_OMIT_LOAD_EXTENSION
           15  +COPTS += -DSQLITE_WITHOUT_ZONEMALLOC
           16  +COPTS += -DSQLITE_ENABLE_RTREE
           17  +
           18  +sqlite3:	shell.c sqlite3.c
           19  +	$(CC) $(CFLAGS) $(COPTS) -o sqlite3 shell.c sqlite3.c $(LIBS)

Changes to ext/expert/expert1.test.

   282    282     CREATE TRIGGER t9t AFTER INSERT ON t9 BEGIN
   283    283       UPDATE t10 SET a=new.a WHERE b = new.b;
   284    284     END;
   285    285   } {
   286    286     INSERT INTO t9 VALUES(?, ?, ?);
   287    287   } {
   288    288     CREATE INDEX t10_idx_00000062 ON t10(b); 
   289         -  -- TRIGGER t9t
   290    289     SEARCH TABLE t10 USING INDEX t10_idx_00000062 (b=?)
   291    290   }
   292    291   
   293    292   do_setup_rec_test $tn.15 {
   294    293     CREATE TABLE t1(a, b);
   295    294     CREATE TABLE t2(c, d);
   296    295   

Changes to ext/expert/sqlite3expert.c.

  1148   1148               idxHashAdd(&rc, &hIdx, zSql, 0);
  1149   1149               if( rc ) goto find_indexes_out;
  1150   1150             }
  1151   1151             break;
  1152   1152           }
  1153   1153         }
  1154   1154   
  1155         -      pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
         1155  +      if( zDetail[0]!='-' ){
         1156  +        pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
         1157  +      }
  1156   1158       }
  1157   1159   
  1158   1160       for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
  1159   1161         pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
  1160   1162       }
  1161   1163   
  1162   1164       idxFinalize(&rc, pExplain);

Changes to ext/misc/csv.c.

   201    201   **   +  Input comes from p->in.
   202    202   **   +  Store results in p->z of length p->n.  Space to hold p->z comes
   203    203   **      from sqlite3_malloc64().
   204    204   **   +  Keep track of the line number in p->nLine.
   205    205   **   +  Store the character that terminates the field in p->cTerm.  Store
   206    206   **      EOF on end-of-file.
   207    207   **
   208         -** Return "" at EOF.  Return 0 on an OOM error.
          208  +** Return 0 at EOF or on OOM.  On EOF, the p->cTerm character will have
          209  +** been set to EOF.
   209    210   */
   210    211   static char *csv_read_one_field(CsvReader *p){
   211    212     int c;
   212    213     p->n = 0;
   213    214     c = csv_getc(p);
   214    215     if( c==EOF ){
   215    216       p->cTerm = EOF;
   216         -    return "";
          217  +    return 0;
   217    218     }
   218    219     if( c=='"' ){
   219    220       int pc, ppc;
   220    221       int startLine = p->nLine;
   221    222       pc = ppc = 0;
   222    223       while( 1 ){
   223    224         c = csv_getc(p);
................................................................................
   540    541     *ppVtab = (sqlite3_vtab*)pNew;
   541    542     if( pNew==0 ) goto csvtab_connect_oom;
   542    543     memset(pNew, 0, sizeof(*pNew));
   543    544     if( nCol>0 ){
   544    545       pNew->nCol = nCol;
   545    546     }else{
   546    547       do{
   547         -      const char *z = csv_read_one_field(&sRdr);
   548         -      if( z==0 ) goto csvtab_connect_oom;
          548  +      csv_read_one_field(&sRdr);
   549    549         pNew->nCol++;
   550    550       }while( sRdr.cTerm==',' );
   551    551     }
   552    552     pNew->zFilename = CSV_FILENAME;  CSV_FILENAME = 0;
   553    553     pNew->zData = CSV_DATA;          CSV_DATA = 0;
   554    554   #ifdef SQLITE_TEST
   555    555     pNew->tstFlags = tstFlags;
................................................................................
   659    659     CsvCursor *pCur = (CsvCursor*)cur;
   660    660     CsvTable *pTab = (CsvTable*)cur->pVtab;
   661    661     int i = 0;
   662    662     char *z;
   663    663     do{
   664    664       z = csv_read_one_field(&pCur->rdr);
   665    665       if( z==0 ){
   666         -      csv_xfer_error(pTab, &pCur->rdr);
   667    666         break;
   668    667       }
   669    668       if( i<pTab->nCol ){
   670    669         if( pCur->aLen[i] < pCur->rdr.n+1 ){
   671    670           char *zNew = sqlite3_realloc64(pCur->azVal[i], pCur->rdr.n+1);
   672    671           if( zNew==0 ){
   673    672             csv_errmsg(&pCur->rdr, "out of memory");

Changes to src/auth.c.

   146    146     sqlite3 *db = pParse->db;
   147    147     Table *pTab = 0;      /* The table being read */
   148    148     const char *zCol;     /* Name of the column of the table */
   149    149     int iSrc;             /* Index in pTabList->a[] of table being read */
   150    150     int iDb;              /* The index of the database the expression refers to */
   151    151     int iCol;             /* Index of column in table */
   152    152   
          153  +  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
   153    154     if( db->xAuth==0 ) return;
   154    155     iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
   155    156     if( iDb<0 ){
   156    157       /* An attempt to read a column out of a subquery or other
   157    158       ** temporary table. */
   158    159       return;
   159    160     }
   160    161   
   161         -  assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
   162    162     if( pExpr->op==TK_TRIGGER ){
   163    163       pTab = pParse->pTriggerTab;
   164    164     }else{
   165    165       assert( pTabList );
   166    166       for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
   167    167         if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
   168    168           pTab = pTabList->a[iSrc].pTab;

Changes to src/btree.c.

   860    860   **
   861    861   ** Calling this routine with a NULL cursor pointer returns false.
   862    862   **
   863    863   ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor
   864    864   ** back to where it ought to be if this routine returns true.
   865    865   */
   866    866   int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
   867         -  return pCur->eState!=CURSOR_VALID;
          867  +  assert( EIGHT_BYTE_ALIGNMENT(pCur)
          868  +       || pCur==sqlite3BtreeFakeValidCursor() );
          869  +  assert( offsetof(BtCursor, eState)==0 );
          870  +  assert( sizeof(pCur->eState)==1 );
          871  +  return CURSOR_VALID != *(u8*)pCur;
   868    872   }
   869    873   
   870    874   /*
   871    875   ** Return a pointer to a fake BtCursor object that will always answer
   872    876   ** false to the sqlite3BtreeCursorHasMoved() routine above.  The fake
   873    877   ** cursor returned must not be used with any other Btree interface.
   874    878   */

Changes to src/build.c.

  2839   2839                       sqlite3KeyInfoRef(pKey), P4_KEYINFO);
  2840   2840   
  2841   2841     /* Open the table. Loop through all rows of the table, inserting index
  2842   2842     ** records into the sorter. */
  2843   2843     sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
  2844   2844     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
  2845   2845     regRecord = sqlite3GetTempReg(pParse);
         2846  +  sqlite3MultiWrite(pParse);
  2846   2847   
  2847   2848     sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
  2848   2849     sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
  2849   2850     sqlite3ResolvePartIdxLabel(pParse, iPartIdxLabel);
  2850   2851     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
  2851   2852     sqlite3VdbeJumpHere(v, addr1);
  2852   2853     if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
  2853   2854     sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, 
  2854   2855                       (char *)pKey, P4_KEYINFO);
  2855   2856     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
  2856   2857   
  2857   2858     addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
  2858   2859     if( IsUniqueIndex(pIndex) ){
  2859         -    int j2 = sqlite3VdbeCurrentAddr(v) + 3;
  2860         -    sqlite3VdbeGoto(v, j2);
         2860  +    int j2 = sqlite3VdbeGoto(v, 1);
  2861   2861       addr2 = sqlite3VdbeCurrentAddr(v);
         2862  +    sqlite3VdbeVerifyAbortable(v, OE_Abort);
  2862   2863       sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
  2863   2864                            pIndex->nKeyCol); VdbeCoverage(v);
  2864   2865       sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
         2866  +    sqlite3VdbeJumpHere(v, j2);
  2865   2867     }else{
  2866   2868       addr2 = sqlite3VdbeCurrentAddr(v);
  2867   2869     }
  2868   2870     sqlite3VdbeAddOp3(v, OP_SorterData, iSorter, regRecord, iIdx);
  2869   2871     sqlite3VdbeAddOp1(v, OP_SeekEnd, iIdx);
  2870   2872     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
  2871   2873     sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);

Changes to src/callback.c.

   402    402     /* If the createFlag parameter is true and the search did not reveal an
   403    403     ** exact match for the name, number of arguments and encoding, then add a
   404    404     ** new entry to the hash table and return it.
   405    405     */
   406    406     if( createFlag && bestScore<FUNC_PERFECT_MATCH && 
   407    407         (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
   408    408       FuncDef *pOther;
          409  +    u8 *z;
   409    410       pBest->zName = (const char*)&pBest[1];
   410    411       pBest->nArg = (u16)nArg;
   411    412       pBest->funcFlags = enc;
   412    413       memcpy((char*)&pBest[1], zName, nName+1);
          414  +    for(z=(u8*)pBest->zName; *z; z++) *z = sqlite3UpperToLower[*z];
   413    415       pOther = (FuncDef*)sqlite3HashInsert(&db->aFunc, pBest->zName, pBest);
   414    416       if( pOther==pBest ){
   415    417         sqlite3DbFree(db, pBest);
   416    418         sqlite3OomFault(db);
   417    419         return 0;
   418    420       }else{
   419    421         pBest->pNext = pOther;

Changes to src/expr.c.

  4022   4022         exprCodeBetween(pParse, pExpr, target, 0, 0);
  4023   4023         return target;
  4024   4024       }
  4025   4025       case TK_SPAN:
  4026   4026       case TK_COLLATE: 
  4027   4027       case TK_UPLUS: {
  4028   4028         pExpr = pExpr->pLeft;
  4029         -      goto expr_code_doover;
         4029  +      goto expr_code_doover; /* 2018-04-28: Prevent deep recursion. OSSFuzz. */
  4030   4030       }
  4031   4031   
  4032   4032       case TK_TRIGGER: {
  4033   4033         /* If the opcode is TK_TRIGGER, then the expression is a reference
  4034   4034         ** to a column in the new.* or old.* pseudo-tables available to
  4035   4035         ** trigger programs. In this case Expr.iTable is set to 1 for the
  4036   4036         ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn

Changes to src/fkey.c.

   327    327     int isIgnore          /* If true, pretend pTab contains all NULL values */
   328    328   ){
   329    329     int i;                                    /* Iterator variable */
   330    330     Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
   331    331     int iCur = pParse->nTab - 1;              /* Cursor number to use */
   332    332     int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
   333    333   
          334  +  sqlite3VdbeVerifyAbortable(v,
          335  +    (!pFKey->isDeferred
          336  +      && !(pParse->db->flags & SQLITE_DeferFKs)
          337  +      && !pParse->pToplevel 
          338  +      && !pParse->isMultiWrite) ? OE_Abort : OE_Ignore);
          339  +
   334    340     /* If nIncr is less than zero, then check at runtime if there are any
   335    341     ** outstanding constraints to resolve. If there are not, there is no need
   336    342     ** to check if deleting this row resolves any outstanding violations.
   337    343     **
   338    344     ** Check if any of the key columns in the child table row are NULL. If 
   339    345     ** any are, then the constraint is considered satisfied. No need to 
   340    346     ** search for a matching row in the parent table.  */
................................................................................
   734    740       ** transactions are not able to rollback schema changes.  
   735    741       **
   736    742       ** If the SQLITE_DeferFKs flag is set, then this is not required, as
   737    743       ** the statement transaction will not be rolled back even if FK
   738    744       ** constraints are violated.
   739    745       */
   740    746       if( (db->flags & SQLITE_DeferFKs)==0 ){
          747  +      sqlite3VdbeVerifyAbortable(v, OE_Abort);
   741    748         sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
   742    749         VdbeCoverage(v);
   743    750         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
   744    751             OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
   745    752       }
   746    753   
   747    754       if( iSkip ){

Changes to src/insert.c.

  1416   1416       pParse->iSelfTab = -(regNewData+1);
  1417   1417       onError = overrideError!=OE_Default ? overrideError : OE_Abort;
  1418   1418       for(i=0; i<pCheck->nExpr; i++){
  1419   1419         int allOk;
  1420   1420         Expr *pExpr = pCheck->a[i].pExpr;
  1421   1421         if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
  1422   1422         allOk = sqlite3VdbeMakeLabel(v);
         1423  +      sqlite3VdbeVerifyAbortable(v, onError);
  1423   1424         sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
  1424   1425         if( onError==OE_Ignore ){
  1425   1426           sqlite3VdbeGoto(v, ignoreDest);
  1426   1427         }else{
  1427   1428           char *zName = pCheck->a[i].zName;
  1428   1429           if( zName==0 ) zName = pTab->zName;
  1429   1430           if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
................................................................................
  1525   1526         sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
  1526   1527         VdbeCoverage(v);
  1527   1528       }
  1528   1529   
  1529   1530       /* Check to see if the new rowid already exists in the table.  Skip
  1530   1531       ** the following conflict logic if it does not. */
  1531   1532       VdbeNoopComment((v, "uniqueness check for ROWID"));
         1533  +    sqlite3VdbeVerifyAbortable(v, onError);
  1532   1534       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
  1533   1535       VdbeCoverage(v);
  1534   1536   
  1535   1537       switch( onError ){
  1536   1538         default: {
  1537   1539           onError = OE_Abort;
  1538   1540           /* Fall thru into the next case */
................................................................................
  1737   1739       ){
  1738   1740         sqlite3VdbeResolveLabel(v, addrUniqueOk);
  1739   1741         continue;
  1740   1742       }
  1741   1743   
  1742   1744       /* Check to see if the new index entry will be unique */
  1743   1745       sqlite3ExprCachePush(pParse);
         1746  +    sqlite3VdbeVerifyAbortable(v, onError);
  1744   1747       sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
  1745   1748                            regIdx, pIdx->nKeyCol); VdbeCoverage(v);
  1746   1749   
  1747   1750       /* Generate code to handle collisions */
  1748   1751       regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
  1749   1752       if( isUpdate || onError==OE_Replace ){
  1750   1753         if( HasRowid(pTab) ){
................................................................................
  1823   1826           testcase( onError==OE_Ignore );
  1824   1827           sqlite3VdbeGoto(v, ignoreDest);
  1825   1828           break;
  1826   1829         }
  1827   1830         default: {
  1828   1831           Trigger *pTrigger = 0;
  1829   1832           assert( onError==OE_Replace );
  1830         -        sqlite3MultiWrite(pParse);
  1831   1833           if( db->flags&SQLITE_RecTriggers ){
  1832   1834             pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
         1835  +        }
         1836  +        if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
         1837  +          sqlite3MultiWrite(pParse);
  1833   1838           }
  1834   1839           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
  1835   1840               regR, nPkField, 0, OE_Replace,
  1836   1841               (pIdx==pPk ? ONEPASS_SINGLE : ONEPASS_OFF), iThisCur);
  1837   1842           seenReplace = 1;
  1838   1843           break;
  1839   1844         }
................................................................................
  2346   2351     }
  2347   2352     if( HasRowid(pSrc) ){
  2348   2353       u8 insFlags;
  2349   2354       sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
  2350   2355       emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
  2351   2356       if( pDest->iPKey>=0 ){
  2352   2357         addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
         2358  +      sqlite3VdbeVerifyAbortable(v, onError);
  2353   2359         addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
  2354   2360         VdbeCoverage(v);
  2355   2361         sqlite3RowidConstraint(pParse, onError, pDest);
  2356   2362         sqlite3VdbeJumpHere(v, addr2);
  2357   2363         autoIncStep(pParse, regAutoinc, regRowid);
  2358   2364       }else if( pDest->pIndex==0 ){
  2359   2365         addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);

Changes to src/main.c.

  1804   1804   #ifdef SQLITE_ENABLE_API_ARMOR
  1805   1805     if( !sqlite3SafetyCheckOk(db) ){
  1806   1806       return SQLITE_MISUSE_BKPT;
  1807   1807     }
  1808   1808   #endif
  1809   1809     sqlite3_mutex_enter(db->mutex);
  1810   1810     if( xDestroy ){
  1811         -    pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
         1811  +    pArg = (FuncDestructor *)sqlite3Malloc(sizeof(FuncDestructor));
  1812   1812       if( !pArg ){
         1813  +      sqlite3OomFault(db);
  1813   1814         xDestroy(p);
  1814   1815         goto out;
  1815   1816       }
         1817  +    pArg->nRef = 0;
  1816   1818       pArg->xDestroy = xDestroy;
  1817   1819       pArg->pUserData = p;
  1818   1820     }
  1819   1821     rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xSFunc, xStep, xFinal, pArg);
  1820   1822     if( pArg && pArg->nRef==0 ){
  1821   1823       assert( rc!=SQLITE_OK );
  1822   1824       xDestroy(p);
  1823         -    sqlite3DbFree(db, pArg);
         1825  +    sqlite3_free(pArg);
  1824   1826     }
  1825   1827   
  1826   1828    out:
  1827   1829     rc = sqlite3ApiExit(db, rc);
  1828   1830     sqlite3_mutex_leave(db->mutex);
  1829   1831     return rc;
  1830   1832   }
................................................................................
  1853   1855     sqlite3DbFree(db, zFunc8);
  1854   1856     rc = sqlite3ApiExit(db, rc);
  1855   1857     sqlite3_mutex_leave(db->mutex);
  1856   1858     return rc;
  1857   1859   }
  1858   1860   #endif
  1859   1861   
         1862  +
         1863  +/*
         1864  +** The following is the implementation of an SQL function that always
         1865  +** fails with an error message stating that the function is used in the
         1866  +** wrong context.  The sqlite3_overload_function() API might construct
         1867  +** SQL function that use this routine so that the functions will exist
         1868  +** for name resolution but are actually overloaded by the xFindFunction
         1869  +** method of virtual tables.
         1870  +*/
         1871  +static void sqlite3InvalidFunction(
         1872  +  sqlite3_context *context,  /* The function calling context */
         1873  +  int NotUsed,               /* Number of arguments to the function */
         1874  +  sqlite3_value **NotUsed2   /* Value of each argument */
         1875  +){
         1876  +  const char *zName = (const char*)sqlite3_user_data(context);
         1877  +  char *zErr;
         1878  +  UNUSED_PARAMETER2(NotUsed, NotUsed2);
         1879  +  zErr = sqlite3_mprintf(
         1880  +      "unable to use function %s in the requested context", zName);
         1881  +  sqlite3_result_error(context, zErr, -1);
         1882  +  sqlite3_free(zErr);
         1883  +}
  1860   1884   
  1861   1885   /*
  1862   1886   ** Declare that a function has been overloaded by a virtual table.
  1863   1887   **
  1864   1888   ** If the function already exists as a regular global function, then
  1865   1889   ** this routine is a no-op.  If the function does not exist, then create
  1866   1890   ** a new one that always throws a run-time error.  
................................................................................
  1871   1895   ** properly.
  1872   1896   */
  1873   1897   int sqlite3_overload_function(
  1874   1898     sqlite3 *db,
  1875   1899     const char *zName,
  1876   1900     int nArg
  1877   1901   ){
  1878         -  int rc = SQLITE_OK;
         1902  +  int rc;
         1903  +  char *zCopy;
  1879   1904   
  1880   1905   #ifdef SQLITE_ENABLE_API_ARMOR
  1881   1906     if( !sqlite3SafetyCheckOk(db) || zName==0 || nArg<-2 ){
  1882   1907       return SQLITE_MISUSE_BKPT;
  1883   1908     }
  1884   1909   #endif
  1885   1910     sqlite3_mutex_enter(db->mutex);
  1886         -  if( sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)==0 ){
  1887         -    rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
  1888         -                           0, sqlite3InvalidFunction, 0, 0, 0);
  1889         -  }
  1890         -  rc = sqlite3ApiExit(db, rc);
         1911  +  rc = sqlite3FindFunction(db, zName, nArg, SQLITE_UTF8, 0)!=0;
  1891   1912     sqlite3_mutex_leave(db->mutex);
  1892         -  return rc;
         1913  +  if( rc ) return SQLITE_OK;
         1914  +  zCopy = sqlite3_mprintf(zName);
         1915  +  if( zCopy==0 ) return SQLITE_NOMEM;
         1916  +  return sqlite3_create_function_v2(db, zName, nArg, SQLITE_UTF8,
         1917  +                           zCopy, sqlite3InvalidFunction, 0, 0, sqlite3_free);
  1893   1918   }
  1894   1919   
  1895   1920   #ifndef SQLITE_OMIT_TRACE
  1896   1921   /*
  1897   1922   ** Register a trace function.  The pArg from the previously registered trace
  1898   1923   ** is returned.  
  1899   1924   **

Changes to src/os_unix.c.

  3775   3775         /* The code below is handling the return value of osFallocate() 
  3776   3776         ** correctly. posix_fallocate() is defined to "returns zero on success, 
  3777   3777         ** or an error number on  failure". See the manpage for details. */
  3778   3778         int err;
  3779   3779         do{
  3780   3780           err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
  3781   3781         }while( err==EINTR );
  3782         -      if( err ) return SQLITE_IOERR_WRITE;
         3782  +      if( err && err!=EINVAL ) return SQLITE_IOERR_WRITE;
  3783   3783   #else
  3784   3784         /* If the OS does not have posix_fallocate(), fake it. Write a 
  3785   3785         ** single byte to the last byte in each block that falls entirely
  3786   3786         ** within the extended region. Then, if required, a single byte
  3787   3787         ** at offset (nSize-1), to set the size of the file correctly.
  3788   3788         ** This is a similar technique to that used by glibc on systems
  3789   3789         ** that do not have a real fallocate() call.

Changes to src/parse.y.

   465    465         ){
   466    466           sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
   467    467         }
   468    468       }
   469    469     }
   470    470   }
   471    471   
          472  +%ifndef SQLITE_OMIT_CTE
   472    473   select(A) ::= WITH wqlist(W) selectnowith(X). {
   473    474     Select *p = X;
   474    475     if( p ){
   475    476       p->pWith = W;
   476    477       parserDoubleLinkSelect(pParse, p);
   477    478     }else{
   478    479       sqlite3WithDelete(pParse->db, W);
................................................................................
   485    486       p->pWith = W;
   486    487       parserDoubleLinkSelect(pParse, p);
   487    488     }else{
   488    489       sqlite3WithDelete(pParse->db, W);
   489    490     }
   490    491     A = p;
   491    492   }
          493  +%endif /* SQLITE_OMIT_CTE */
   492    494   select(A) ::= selectnowith(X). {
   493    495     Select *p = X;
   494    496     if( p ){
   495    497       parserDoubleLinkSelect(pParse, p);
   496    498     }
   497    499     A = p; /*A-overwrites-X*/
   498    500   }

Changes to src/printf.c.

   970    970   /*
   971    971   ** This singleton is an sqlite3_str object that is returned if
   972    972   ** sqlite3_malloc() fails to provide space for a real one.  This
   973    973   ** sqlite3_str object accepts no new text and always returns
   974    974   ** an SQLITE_NOMEM error.
   975    975   */
   976    976   static sqlite3_str sqlite3OomStr = {
   977         -   0, 0, 0, 0, 0, SQLITE_NOMEM
          977  +   0, 0, 0, 0, 0, SQLITE_NOMEM, 0
   978    978   };
   979    979   
   980    980   /* Finalize a string created using sqlite3_str_new().
   981    981   */
   982    982   char *sqlite3_str_finish(sqlite3_str *p){
   983    983     char *z;
   984    984     if( p!=0 && p!=&sqlite3OomStr ){

Changes to src/resolve.c.

    71     71     sqlite3 *db;           /* The database connection */
    72     72   
    73     73     assert( iCol>=0 && iCol<pEList->nExpr );
    74     74     pOrig = pEList->a[iCol].pExpr;
    75     75     assert( pOrig!=0 );
    76     76     db = pParse->db;
    77     77     pDup = sqlite3ExprDup(db, pOrig, 0);
    78         -  if( pDup==0 ) return;
    79         -  if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
    80         -  if( pExpr->op==TK_COLLATE ){
    81         -    pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
    82         -  }
    83         -  ExprSetProperty(pDup, EP_Alias);
    84         -
    85         -  /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
    86         -  ** prevents ExprDelete() from deleting the Expr structure itself,
    87         -  ** allowing it to be repopulated by the memcpy() on the following line.
    88         -  ** The pExpr->u.zToken might point into memory that will be freed by the
    89         -  ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
    90         -  ** make a copy of the token before doing the sqlite3DbFree().
    91         -  */
    92         -  ExprSetProperty(pExpr, EP_Static);
    93         -  sqlite3ExprDelete(db, pExpr);
    94         -  memcpy(pExpr, pDup, sizeof(*pExpr));
    95         -  if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
    96         -    assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
    97         -    pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
    98         -    pExpr->flags |= EP_MemToken;
    99         -  }
   100         -  sqlite3DbFree(db, pDup);
           78  +  if( pDup!=0 ){
           79  +    if( zType[0]!='G' ) incrAggFunctionDepth(pDup, nSubquery);
           80  +    if( pExpr->op==TK_COLLATE ){
           81  +      pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
           82  +    }
           83  +    ExprSetProperty(pDup, EP_Alias);
           84  +
           85  +    /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This 
           86  +    ** prevents ExprDelete() from deleting the Expr structure itself,
           87  +    ** allowing it to be repopulated by the memcpy() on the following line.
           88  +    ** The pExpr->u.zToken might point into memory that will be freed by the
           89  +    ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
           90  +    ** make a copy of the token before doing the sqlite3DbFree().
           91  +    */
           92  +    ExprSetProperty(pExpr, EP_Static);
           93  +    sqlite3ExprDelete(db, pExpr);
           94  +    memcpy(pExpr, pDup, sizeof(*pExpr));
           95  +    if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
           96  +      assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
           97  +      pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
           98  +      pExpr->flags |= EP_MemToken;
           99  +    }
          100  +    sqlite3DbFree(db, pDup);
          101  +  }
          102  +  ExprSetProperty(pExpr, EP_Alias);
   101    103   }
   102    104   
   103    105   
   104    106   /*
   105    107   ** Return TRUE if the name zCol occurs anywhere in the USING clause.
   106    108   **
   107    109   ** Return FALSE if the USING clause is NULL or if it does not contain
................................................................................
   345    347           if( iCol<pTab->nCol ){
   346    348             cnt++;
   347    349   #ifndef SQLITE_OMIT_UPSERT
   348    350             if( pExpr->iTable==2 ){
   349    351               testcase( iCol==(-1) );
   350    352               pExpr->iTable = pNC->uNC.pUpsert->regData + iCol;
   351    353               eNewExprOp = TK_REGISTER;
          354  +            ExprSetProperty(pExpr, EP_Alias);
   352    355             }else
   353    356   #endif /* SQLITE_OMIT_UPSERT */
   354    357             {
   355    358   #ifndef SQLITE_OMIT_TRIGGER
   356    359               if( iCol<0 ){
   357    360                 pExpr->affinity = SQLITE_AFF_INTEGER;
   358    361               }else if( pExpr->iTable==0 ){

Changes to src/select.c.

   806    806     int nDefer = 0;
   807    807     ExprList *pExtra = 0;
   808    808     for(i=0; i<pEList->nExpr; i++){
   809    809       struct ExprList_item *pItem = &pEList->a[i];
   810    810       if( pItem->u.x.iOrderByCol==0 ){
   811    811         Expr *pExpr = pItem->pExpr;
   812    812         Table *pTab = pExpr->pTab;
   813         -      if( pExpr->op==TK_COLUMN && pTab && !IsVirtual(pTab)
          813  +      if( pExpr->op==TK_COLUMN && pExpr->iColumn>=0 && pTab && !IsVirtual(pTab)
   814    814          && (pTab->aCol[pExpr->iColumn].colFlags & COLFLAG_SORTERREF)
   815    815         ){
   816    816           int j;
   817    817           for(j=0; j<nDefer; j++){
   818    818             if( pSort->aDefer[j].iCsr==pExpr->iTable ) break;
   819    819           }
   820    820           if( j==nDefer ){

Changes to src/shell.c.in.

  8239   8239   
  8240   8240   /*
  8241   8241   ** Internal check:  Verify that the SQLite is uninitialized.  Print a
  8242   8242   ** error message if it is initialized.
  8243   8243   */
  8244   8244   static void verify_uninitialized(void){
  8245   8245     if( sqlite3_config(-1)==SQLITE_MISUSE ){
  8246         -    utf8_printf(stdout, "WARNING: attempt to configuration SQLite after"
         8246  +    utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
  8247   8247                           " initialization.\n");
  8248   8248     }
  8249   8249   }
  8250   8250   
  8251   8251   /*
  8252   8252   ** Initialize the state information in data
  8253   8253   */

Changes to src/sqlite.h.in.

  6290   6290     int idxFlags;              /* Mask of SQLITE_INDEX_SCAN_* flags */
  6291   6291     /* Fields below are only available in SQLite 3.10.0 and later */
  6292   6292     sqlite3_uint64 colUsed;    /* Input: Mask of columns used by statement */
  6293   6293   };
  6294   6294   
  6295   6295   /*
  6296   6296   ** CAPI3REF: Virtual Table Scan Flags
         6297  +**
         6298  +** Virtual table implementations are allowed to set the 
         6299  +** [sqlite3_index_info].idxFlags field to some combination of
         6300  +** these bits.
  6297   6301   */
  6298   6302   #define SQLITE_INDEX_SCAN_UNIQUE      1     /* Scan visits at most 1 row */
  6299   6303   
  6300   6304   /*
  6301   6305   ** CAPI3REF: Virtual Table Constraint Operator Codes
  6302   6306   **
  6303   6307   ** These macros defined the allowed values for the
................................................................................
  9030   9034   ** to the contiguous memory representation of the database that SQLite
  9031   9035   ** is currently using for that database, or NULL if the no such contiguous
  9032   9036   ** memory representation of the database exists.  A contiguous memory
  9033   9037   ** representation of the database will usually only exist if there has
  9034   9038   ** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
  9035   9039   ** values of D and S.
  9036   9040   ** The size of the database is written into *P even if the 
  9037         -** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         9041  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
  9038   9042   ** of the database exists.
  9039   9043   **
  9040   9044   ** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
  9041   9045   ** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
  9042   9046   ** allocation error occurs.
  9043   9047   **
  9044   9048   ** This interface is only available if SQLite is compiled with the

Changes to src/sqliteInt.h.

  4221   4221   int sqlite3KeyInfoIsWriteable(KeyInfo*);
  4222   4222   #endif
  4223   4223   int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
  4224   4224     void (*)(sqlite3_context*,int,sqlite3_value **),
  4225   4225     void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
  4226   4226     FuncDestructor *pDestructor
  4227   4227   );
         4228  +void sqlite3NoopDestructor(void*);
  4228   4229   void sqlite3OomFault(sqlite3*);
  4229   4230   void sqlite3OomClear(sqlite3*);
  4230   4231   int sqlite3ApiExit(sqlite3 *db, int);
  4231   4232   int sqlite3OpenTempDatabase(Parse *);
  4232   4233   
  4233   4234   void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
  4234   4235   char *sqlite3StrAccumFinish(StrAccum*);
................................................................................
  4323   4324   void sqlite3VtabArgInit(Parse*);
  4324   4325   void sqlite3VtabArgExtend(Parse*, Token*);
  4325   4326   int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
  4326   4327   int sqlite3VtabCallConnect(Parse*, Table*);
  4327   4328   int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
  4328   4329   int sqlite3VtabBegin(sqlite3 *, VTable *);
  4329   4330   FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
  4330         -void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
  4331   4331   sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
  4332   4332   int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
  4333   4333   int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
  4334   4334   void sqlite3ParserReset(Parse*);
  4335   4335   int sqlite3Reprepare(Vdbe*);
  4336   4336   void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
  4337   4337   CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);

Changes to src/upsert.c.

   225    225           int k;
   226    226           assert( pPk->aiColumn[i]>=0 );
   227    227           k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
   228    228           sqlite3VdbeAddOp3(v, OP_Column, iCur, k, iPk+i);
   229    229           VdbeComment((v, "%s.%s", pIdx->zName,
   230    230                       pTab->aCol[pPk->aiColumn[i]].zName));
   231    231         }
          232  +      sqlite3VdbeVerifyAbortable(v, OE_Abort);
   232    233         i = sqlite3VdbeAddOp4Int(v, OP_Found, iDataCur, 0, iPk, nPk);
   233    234         VdbeCoverage(v);
   234    235         sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CORRUPT, OE_Abort, 0, 
   235    236               "corrupt database", P4_STATIC);
   236    237         sqlite3VdbeJumpHere(v, i);
   237    238       }
   238    239     }

Changes to src/vdbe.c.

   911    911   ** Check the value in register P3.  If it is NULL then Halt using
   912    912   ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
   913    913   ** value in register P3 is not NULL, then this routine is a no-op.
   914    914   ** The P5 parameter should be 1.
   915    915   */
   916    916   case OP_HaltIfNull: {      /* in3 */
   917    917     pIn3 = &aMem[pOp->p3];
          918  +#ifdef SQLITE_DEBUG
          919  +  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
          920  +#endif
   918    921     if( (pIn3->flags & MEM_Null)==0 ) break;
   919    922     /* Fall through into OP_Halt */
   920    923   }
   921    924   
   922    925   /* Opcode:  Halt P1 P2 * P4 P5
   923    926   **
   924    927   ** Exit immediately.  All open cursors, etc are closed
................................................................................
   950    953   ** is the same as executing Halt.
   951    954   */
   952    955   case OP_Halt: {
   953    956     VdbeFrame *pFrame;
   954    957     int pcx;
   955    958   
   956    959     pcx = (int)(pOp - aOp);
          960  +#ifdef SQLITE_DEBUG
          961  +  if( pOp->p2==OE_Abort ){ sqlite3VdbeAssertAbortable(p); }
          962  +#endif
   957    963     if( pOp->p1==SQLITE_OK && p->pFrame ){
   958    964       /* Halt the sub-program. Return control to the parent frame. */
   959    965       pFrame = p->pFrame;
   960    966       p->pFrame = pFrame->pParent;
   961    967       p->nFrame--;
   962    968       sqlite3VdbeSetChanges(db, p->nChange);
   963    969       pcx = sqlite3VdbeFrameRestore(pFrame);
................................................................................
  3320   3326   ** size, and so forth.  P1==0 is the main database file and P1==1 is the 
  3321   3327   ** database file used to store temporary tables.
  3322   3328   **
  3323   3329   ** A transaction must be started before executing this opcode.
  3324   3330   */
  3325   3331   case OP_SetCookie: {
  3326   3332     Db *pDb;
         3333  +
         3334  +  sqlite3VdbeIncrWriteCounter(p, 0);
  3327   3335     assert( pOp->p2<SQLITE_N_BTREE_META );
  3328   3336     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  3329   3337     assert( DbMaskTest(p->btreeMask, pOp->p1) );
  3330   3338     assert( p->readOnly==0 );
  3331   3339     pDb = &db->aDb[pOp->p1];
  3332   3340     assert( pDb->pBt!=0 );
  3333   3341     assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
................................................................................
  4453   4461     pC = p->apCsr[pOp->p1];
  4454   4462     assert( pC!=0 );
  4455   4463     assert( pC->eCurType==CURTYPE_BTREE );
  4456   4464     assert( pC->uc.pCursor!=0 );
  4457   4465     assert( (pOp->p5 & OPFLAG_ISNOOP) || pC->isTable );
  4458   4466     assert( pOp->p4type==P4_TABLE || pOp->p4type>=P4_STATIC );
  4459   4467     REGISTER_TRACE(pOp->p2, pData);
         4468  +  sqlite3VdbeIncrWriteCounter(p, pC);
  4460   4469   
  4461   4470     if( pOp->opcode==OP_Insert ){
  4462   4471       pKey = &aMem[pOp->p3];
  4463   4472       assert( pKey->flags & MEM_Int );
  4464   4473       assert( memIsValid(pKey) );
  4465   4474       REGISTER_TRACE(pOp->p3, pKey);
  4466   4475       x.nKey = pKey->u.i;
................................................................................
  4567   4576     opflags = pOp->p2;
  4568   4577     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  4569   4578     pC = p->apCsr[pOp->p1];
  4570   4579     assert( pC!=0 );
  4571   4580     assert( pC->eCurType==CURTYPE_BTREE );
  4572   4581     assert( pC->uc.pCursor!=0 );
  4573   4582     assert( pC->deferredMoveto==0 );
         4583  +  sqlite3VdbeIncrWriteCounter(p, pC);
  4574   4584   
  4575   4585   #ifdef SQLITE_DEBUG
  4576   4586     if( pOp->p4type==P4_TABLE && HasRowid(pOp->p4.pTab) && pOp->p5==0 ){
  4577   4587       /* If p5 is zero, the seek operation that positioned the cursor prior to
  4578   4588       ** OP_Delete will have also set the pC->movetoTarget field to the rowid of
  4579   4589       ** the row that is being deleted */
  4580   4590       i64 iKey = sqlite3BtreeIntegerKey(pC->uc.pCursor);
................................................................................
  4735   4745   **
  4736   4746   ** If cursor P1 is an index, then the content is the key of the row.
  4737   4747   ** If cursor P2 is a table, then the content extracted is the data.
  4738   4748   **
  4739   4749   ** If the P1 cursor must be pointing to a valid row (not a NULL row)
  4740   4750   ** of a real table, not a pseudo-table.
  4741   4751   **
  4742         -** If P3!=0 then this opcode is allowed to make an ephermeral pointer
         4752  +** If P3!=0 then this opcode is allowed to make an ephemeral pointer
  4743   4753   ** into the database page.  That means that the content of the output
  4744   4754   ** register will be invalidated as soon as the cursor moves - including
  4745         -** moves caused by other cursors that "save" the the current cursors
         4755  +** moves caused by other cursors that "save" the current cursors
  4746   4756   ** position in order that they can write to the same table.  If P3==0
  4747   4757   ** then a copy of the data is made into memory.  P3!=0 is faster, but
  4748   4758   ** P3==0 is safer.
  4749   4759   **
  4750   4760   ** If P3!=0 then the content of the P2 register is unsuitable for use
  4751   4761   ** in OP_Result and any OP_Result will invalidate the P2 register content.
  4752   4762   ** The P2 register content is invalidated by opcodes like OP_Function or
................................................................................
  5186   5196   case OP_SorterInsert:       /* in2 */
  5187   5197   case OP_IdxInsert: {        /* in2 */
  5188   5198     VdbeCursor *pC;
  5189   5199     BtreePayload x;
  5190   5200   
  5191   5201     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  5192   5202     pC = p->apCsr[pOp->p1];
         5203  +  sqlite3VdbeIncrWriteCounter(p, pC);
  5193   5204     assert( pC!=0 );
  5194   5205     assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
  5195   5206     pIn2 = &aMem[pOp->p2];
  5196   5207     assert( pIn2->flags & MEM_Blob );
  5197   5208     if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
  5198   5209     assert( pC->eCurType==CURTYPE_BTREE || pOp->opcode==OP_SorterInsert );
  5199   5210     assert( pC->isTable==0 );
................................................................................
  5232   5243   
  5233   5244     assert( pOp->p3>0 );
  5234   5245     assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem+1 - p->nCursor)+1 );
  5235   5246     assert( pOp->p1>=0 && pOp->p1<p->nCursor );
  5236   5247     pC = p->apCsr[pOp->p1];
  5237   5248     assert( pC!=0 );
  5238   5249     assert( pC->eCurType==CURTYPE_BTREE );
         5250  +  sqlite3VdbeIncrWriteCounter(p, pC);
  5239   5251     pCrsr = pC->uc.pCursor;
  5240   5252     assert( pCrsr!=0 );
  5241   5253     assert( pOp->p5==0 );
  5242   5254     r.pKeyInfo = pC->pKeyInfo;
  5243   5255     r.nField = (u16)pOp->p3;
  5244   5256     r.default_rc = 0;
  5245   5257     r.aMem = &aMem[pOp->p2];
................................................................................
  5454   5466   **
  5455   5467   ** See also: Clear
  5456   5468   */
  5457   5469   case OP_Destroy: {     /* out2 */
  5458   5470     int iMoved;
  5459   5471     int iDb;
  5460   5472   
         5473  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5461   5474     assert( p->readOnly==0 );
  5462   5475     assert( pOp->p1>1 );
  5463   5476     pOut = out2Prerelease(p, pOp);
  5464   5477     pOut->flags = MEM_Null;
  5465   5478     if( db->nVdbeRead > db->nVDestroy+1 ){
  5466   5479       rc = SQLITE_LOCKED;
  5467   5480       p->errorAction = OE_Abort;
................................................................................
  5503   5516   ** also incremented by the number of rows in the table being cleared.
  5504   5517   **
  5505   5518   ** See also: Destroy
  5506   5519   */
  5507   5520   case OP_Clear: {
  5508   5521     int nChange;
  5509   5522    
         5523  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5510   5524     nChange = 0;
  5511   5525     assert( p->readOnly==0 );
  5512   5526     assert( DbMaskTest(p->btreeMask, pOp->p2) );
  5513   5527     rc = sqlite3BtreeClearTable(
  5514   5528         db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
  5515   5529     );
  5516   5530     if( pOp->p3 ){
................................................................................
  5552   5566   
  5553   5567   /* Opcode: CreateBtree P1 P2 P3 * *
  5554   5568   ** Synopsis: r[P2]=root iDb=P1 flags=P3
  5555   5569   **
  5556   5570   ** Allocate a new b-tree in the main database file if P1==0 or in the
  5557   5571   ** TEMP database file if P1==1 or in an attached database if
  5558   5572   ** P1>1.  The P3 argument must be 1 (BTREE_INTKEY) for a rowid table
  5559         -** it must be 2 (BTREE_BLOBKEY) for a index or WITHOUT ROWID table.
         5573  +** it must be 2 (BTREE_BLOBKEY) for an index or WITHOUT ROWID table.
  5560   5574   ** The root page number of the new b-tree is stored in register P2.
  5561   5575   */
  5562   5576   case OP_CreateBtree: {          /* out2 */
  5563   5577     int pgno;
  5564   5578     Db *pDb;
  5565   5579   
         5580  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5566   5581     pOut = out2Prerelease(p, pOp);
  5567   5582     pgno = 0;
  5568   5583     assert( pOp->p3==BTREE_INTKEY || pOp->p3==BTREE_BLOBKEY );
  5569   5584     assert( pOp->p1>=0 && pOp->p1<db->nDb );
  5570   5585     assert( DbMaskTest(p->btreeMask, pOp->p1) );
  5571   5586     assert( p->readOnly==0 );
  5572   5587     pDb = &db->aDb[pOp->p1];
................................................................................
  5578   5593   }
  5579   5594   
  5580   5595   /* Opcode: SqlExec * * * P4 *
  5581   5596   **
  5582   5597   ** Run the SQL statement or statements specified in the P4 string.
  5583   5598   */
  5584   5599   case OP_SqlExec: {
         5600  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5585   5601     db->nSqlExec++;
  5586   5602     rc = sqlite3_exec(db, pOp->p4.z, 0, 0, 0);
  5587   5603     db->nSqlExec--;
  5588   5604     if( rc ) goto abort_due_to_error;
  5589   5605     break;
  5590   5606   }
  5591   5607   
................................................................................
  5667   5683   ** Remove the internal (in-memory) data structures that describe
  5668   5684   ** the table named P4 in database P1.  This is called after a table
  5669   5685   ** is dropped from disk (using the Destroy opcode) in order to keep 
  5670   5686   ** the internal representation of the
  5671   5687   ** schema consistent with what is on disk.
  5672   5688   */
  5673   5689   case OP_DropTable: {
         5690  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5674   5691     sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
  5675   5692     break;
  5676   5693   }
  5677   5694   
  5678   5695   /* Opcode: DropIndex P1 * * P4 *
  5679   5696   **
  5680   5697   ** Remove the internal (in-memory) data structures that describe
  5681   5698   ** the index named P4 in database P1.  This is called after an index
  5682   5699   ** is dropped from disk (using the Destroy opcode)
  5683   5700   ** in order to keep the internal representation of the
  5684   5701   ** schema consistent with what is on disk.
  5685   5702   */
  5686   5703   case OP_DropIndex: {
         5704  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5687   5705     sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
  5688   5706     break;
  5689   5707   }
  5690   5708   
  5691   5709   /* Opcode: DropTrigger P1 * * P4 *
  5692   5710   **
  5693   5711   ** Remove the internal (in-memory) data structures that describe
  5694   5712   ** the trigger named P4 in database P1.  This is called after a trigger
  5695   5713   ** is dropped from disk (using the Destroy opcode) in order to keep 
  5696   5714   ** the internal representation of the
  5697   5715   ** schema consistent with what is on disk.
  5698   5716   */
  5699   5717   case OP_DropTrigger: {
         5718  +  sqlite3VdbeIncrWriteCounter(p, 0);
  5700   5719     sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
  5701   5720     break;
  5702   5721   }
  5703   5722   
  5704   5723   
  5705   5724   #ifndef SQLITE_OMIT_INTEGRITY_CHECK
  5706   5725   /* Opcode: IntegrityCk P1 P2 P3 P4 P5
................................................................................
  6907   6926     Mem **apArg;
  6908   6927     Mem *pX;
  6909   6928   
  6910   6929     assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback 
  6911   6930          || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
  6912   6931     );
  6913   6932     assert( p->readOnly==0 );
         6933  +  sqlite3VdbeIncrWriteCounter(p, 0);
  6914   6934     pVtab = pOp->p4.pVtab->pVtab;
  6915   6935     if( pVtab==0 || NEVER(pVtab->pModule==0) ){
  6916   6936       rc = SQLITE_LOCKED;
  6917   6937       goto abort_due_to_error;
  6918   6938     }
  6919   6939     pModule = pVtab->pModule;
  6920   6940     nArg = pOp->p2;
................................................................................
  7223   7243       assert( pC->eCurType==CURTYPE_BTREE );
  7224   7244       sqlite3BtreeCursorHint(pC->uc.pCursor, BTREE_HINT_RANGE,
  7225   7245                              pOp->p4.pExpr, aMem);
  7226   7246     }
  7227   7247     break;
  7228   7248   }
  7229   7249   #endif /* SQLITE_ENABLE_CURSOR_HINTS */
         7250  +
         7251  +#ifdef SQLITE_DEBUG
         7252  +/* Opcode:  Abortable   * * * * *
         7253  +**
         7254  +** Verify that an Abort can happen.  Assert if an Abort at this point
         7255  +** might cause database corruption.  This opcode only appears in debugging
         7256  +** builds.
         7257  +**
         7258  +** An Abort is safe if either there have been no writes, or if there is
         7259  +** an active statement journal.
         7260  +*/
         7261  +case OP_Abortable: {
         7262  +  sqlite3VdbeAssertAbortable(p);
         7263  +  break;
         7264  +}
         7265  +#endif
  7230   7266   
  7231   7267   /* Opcode: Noop * * * * *
  7232   7268   **
  7233   7269   ** Do nothing.  This instruction is often useful as a jump
  7234   7270   ** destination.
  7235   7271   */
  7236   7272   /*
  7237   7273   ** The magic Explain opcode are only inserted when explain==2 (which
  7238   7274   ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
  7239   7275   ** This opcode records information from the optimizer.  It is the
  7240   7276   ** the same as a no-op.  This opcodesnever appears in a real VM program.
  7241   7277   */
  7242         -default: {          /* This is really OP_Noop and OP_Explain */
         7278  +default: {          /* This is really OP_Noop, OP_Explain */
  7243   7279     assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
         7280  +
  7244   7281     break;
  7245   7282   }
  7246   7283   
  7247   7284   /*****************************************************************************
  7248   7285   ** The cases of the switch statement above this line should all be indented
  7249   7286   ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
  7250   7287   ** readability.  From this point on down, the normal indentation rules are

Changes to src/vdbe.h.

   193    193   #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
   194    194     void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
   195    195     void sqlite3VdbeVerifyNoResultRow(Vdbe *p);
   196    196   #else
   197    197   # define sqlite3VdbeVerifyNoMallocRequired(A,B)
   198    198   # define sqlite3VdbeVerifyNoResultRow(A)
   199    199   #endif
          200  +#if defined(SQLITE_DEBUG)
          201  +  void sqlite3VdbeVerifyAbortable(Vdbe *p, int);
          202  +#else
          203  +# define sqlite3VdbeVerifyAbortable(A,B)
          204  +#endif
   200    205   VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
   201    206   #ifndef SQLITE_OMIT_EXPLAIN
   202    207     void sqlite3VdbeExplain(Parse*,u8,const char*,...);
   203    208     void sqlite3VdbeExplainPop(Parse*);
   204    209     int sqlite3VdbeExplainParent(Parse*);
   205    210   # define ExplainQueryPlan(P)        sqlite3VdbeExplain P
   206    211   # define ExplainQueryPlanPop(P)     sqlite3VdbeExplainPop(P)

Changes to src/vdbeInt.h.

   375    375     VList *pVList;          /* Name of variables */
   376    376   #ifndef SQLITE_OMIT_TRACE
   377    377     i64 startTime;          /* Time when query started - used for profiling */
   378    378   #endif
   379    379     int nOp;                /* Number of instructions in the program */
   380    380   #ifdef SQLITE_DEBUG
   381    381     int rcApp;              /* errcode set by sqlite3_result_error_code() */
          382  +  u32 nWrite;             /* Number of write operations that have occurred */
   382    383   #endif
   383    384     u16 nResColumn;         /* Number of columns in one row of the result set */
   384    385     u8 errorAction;         /* Recovery action to do in case of an error */
   385    386     u8 minWriteFileFormat;  /* Minimum file format for writable database files */
   386    387     u8 prepFlags;           /* SQLITE_PREPARE_* flags */
   387    388     bft expired:1;          /* True if the VM needs to be recompiled */
   388    389     bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
................................................................................
   510    511   void sqlite3VdbeSorterReset(sqlite3 *, VdbeSorter *);
   511    512   void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
   512    513   int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
   513    514   int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *);
   514    515   int sqlite3VdbeSorterRewind(const VdbeCursor *, int *);
   515    516   int sqlite3VdbeSorterWrite(const VdbeCursor *, Mem *);
   516    517   int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
          518  +
          519  +#ifdef SQLITE_DEBUG
          520  +  void sqlite3VdbeIncrWriteCounter(Vdbe*, VdbeCursor*);
          521  +  void sqlite3VdbeAssertAbortable(Vdbe*);
          522  +#else
          523  +# define sqlite3VdbeIncrWriteCounter(V,C)
          524  +# define sqlite3VdbeAssertAbortable(V)
          525  +#endif
   517    526   
   518    527   #if !defined(SQLITE_OMIT_SHARED_CACHE) 
   519    528     void sqlite3VdbeEnter(Vdbe*);
   520    529   #else
   521    530   # define sqlite3VdbeEnter(X)
   522    531   #endif
   523    532   

Changes to src/vdbeapi.c.

   783    783     if( *piTime==0 ){
   784    784       rc = sqlite3OsCurrentTimeInt64(p->pOut->db->pVfs, piTime);
   785    785       if( rc ) *piTime = 0;
   786    786     }
   787    787     return *piTime;
   788    788   }
   789    789   
   790         -/*
   791         -** The following is the implementation of an SQL function that always
   792         -** fails with an error message stating that the function is used in the
   793         -** wrong context.  The sqlite3_overload_function() API might construct
   794         -** SQL function that use this routine so that the functions will exist
   795         -** for name resolution but are actually overloaded by the xFindFunction
   796         -** method of virtual tables.
   797         -*/
   798         -void sqlite3InvalidFunction(
   799         -  sqlite3_context *context,  /* The function calling context */
   800         -  int NotUsed,               /* Number of arguments to the function */
   801         -  sqlite3_value **NotUsed2   /* Value of each argument */
   802         -){
   803         -  const char *zName = context->pFunc->zName;
   804         -  char *zErr;
   805         -  UNUSED_PARAMETER2(NotUsed, NotUsed2);
   806         -  zErr = sqlite3_mprintf(
   807         -      "unable to use function %s in the requested context", zName);
   808         -  sqlite3_result_error(context, zErr, -1);
   809         -  sqlite3_free(zErr);
   810         -}
   811         -
   812    790   /*
   813    791   ** Create a new aggregate context for p and return a pointer to
   814    792   ** its pMem->z element.
   815    793   */
   816    794   static SQLITE_NOINLINE void *createAggContext(sqlite3_context *p, int nByte){
   817    795     Mem *pMem = p->pMem;
   818    796     assert( (pMem->flags & MEM_Agg)==0 );

Changes to src/vdbeaux.c.

   599    599     ** true for this case to prevent the assert() in the callers frame
   600    600     ** from failing.  */
   601    601     return ( v->db->mallocFailed || hasAbort==mayAbort || hasFkCounter
   602    602                 || (hasCreateTable && hasInitCoroutine) );
   603    603   }
   604    604   #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
   605    605   
          606  +#ifdef SQLITE_DEBUG
          607  +/*
          608  +** Increment the nWrite counter in the VDBE if the cursor is not an
          609  +** ephemeral cursor, or if the cursor argument is NULL.
          610  +*/
          611  +void sqlite3VdbeIncrWriteCounter(Vdbe *p, VdbeCursor *pC){
          612  +  if( pC==0
          613  +   || (pC->eCurType!=CURTYPE_SORTER
          614  +       && pC->eCurType!=CURTYPE_PSEUDO
          615  +       && !pC->isEphemeral)
          616  +  ){
          617  +    p->nWrite++;
          618  +  }
          619  +}
          620  +#endif
          621  +
          622  +#ifdef SQLITE_DEBUG
          623  +/*
          624  +** Assert if an Abort at this point in time might result in a corrupt
          625  +** database.
          626  +*/
          627  +void sqlite3VdbeAssertAbortable(Vdbe *p){
          628  +  assert( p->nWrite==0 || p->usesStmtJournal );
          629  +}
          630  +#endif
          631  +
   606    632   /*
   607    633   ** This routine is called after all opcodes have been inserted.  It loops
   608    634   ** through all the opcodes and fixes up some details.
   609    635   **
   610    636   ** (1) For each jump instruction with a negative P2 value (a label)
   611    637   **     resolve the P2 value to an actual address.
   612    638   **
................................................................................
   758    784     int i;
   759    785     for(i=0; i<p->nOp; i++){
   760    786       assert( p->aOp[i].opcode!=OP_ResultRow );
   761    787     }
   762    788   }
   763    789   #endif
   764    790   
          791  +/*
          792  +** Generate code (a single OP_Abortable opcode) that will
          793  +** verify that the VDBE program can safely call Abort in the current
          794  +** context.
          795  +*/
          796  +#if defined(SQLITE_DEBUG)
          797  +void sqlite3VdbeVerifyAbortable(Vdbe *p, int onError){
          798  +  if( onError==OE_Abort ) sqlite3VdbeAddOp0(p, OP_Abortable);
          799  +}
          800  +#endif
          801  +
   765    802   /*
   766    803   ** This function returns a pointer to the array of opcodes associated with
   767    804   ** the Vdbe passed as the first argument. It is the callers responsibility
   768    805   ** to arrange for the returned array to be eventually freed using the 
   769    806   ** vdbeFreeOpArray() function.
   770    807   **
   771    808   ** Before returning, *pnOp is set to the number of entries in the returned
................................................................................
  2991   3028     if( p->aMem ){
  2992   3029       for(i=0; i<p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
  2993   3030     }
  2994   3031   #endif
  2995   3032     sqlite3DbFree(db, p->zErrMsg);
  2996   3033     p->zErrMsg = 0;
  2997   3034     p->pResultSet = 0;
         3035  +#ifdef SQLITE_DEBUG
         3036  +  p->nWrite = 0;
         3037  +#endif
  2998   3038   
  2999   3039     /* Save profiling information from this VDBE run.
  3000   3040     */
  3001   3041   #ifdef VDBE_PROFILE
  3002   3042     {
  3003   3043       FILE *out = fopen("vdbe_profile.out", "a");
  3004   3044       if( out ){

Changes to src/vdbemem.c.

   813    813     }else{
   814    814       pMem->u.i = val;
   815    815       pMem->flags = MEM_Int;
   816    816     }
   817    817   }
   818    818   
   819    819   /* A no-op destructor */
   820         -static void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
          820  +void sqlite3NoopDestructor(void *p){ UNUSED_PARAMETER(p); }
   821    821   
   822    822   /*
   823    823   ** Set the value stored in *pMem should already be a NULL.
   824    824   ** Also store a pointer to go with it.
   825    825   */
   826    826   void sqlite3VdbeMemSetPointer(
   827    827     Mem *pMem,

Changes to src/vtab.c.

  1045   1045     Table *pTab;
  1046   1046     sqlite3_vtab *pVtab;
  1047   1047     sqlite3_module *pMod;
  1048   1048     void (*xSFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
  1049   1049     void *pArg = 0;
  1050   1050     FuncDef *pNew;
  1051   1051     int rc = 0;
  1052         -  char *zLowerName;
  1053         -  unsigned char *z;
  1054         -
  1055   1052   
  1056   1053     /* Check to see the left operand is a column in a virtual table */
  1057   1054     if( NEVER(pExpr==0) ) return pDef;
  1058   1055     if( pExpr->op!=TK_COLUMN ) return pDef;
  1059   1056     pTab = pExpr->pTab;
  1060   1057     if( pTab==0 ) return pDef;
  1061   1058     if( !IsVirtual(pTab) ) return pDef;
................................................................................
  1062   1059     pVtab = sqlite3GetVTable(db, pTab)->pVtab;
  1063   1060     assert( pVtab!=0 );
  1064   1061     assert( pVtab->pModule!=0 );
  1065   1062     pMod = (sqlite3_module *)pVtab->pModule;
  1066   1063     if( pMod->xFindFunction==0 ) return pDef;
  1067   1064    
  1068   1065     /* Call the xFindFunction method on the virtual table implementation
  1069         -  ** to see if the implementation wants to overload this function 
         1066  +  ** to see if the implementation wants to overload this function.
         1067  +  **
         1068  +  ** Though undocumented, we have historically always invoked xFindFunction
         1069  +  ** with an all lower-case function name.  Continue in this tradition to
         1070  +  ** avoid any chance of an incompatibility.
  1070   1071     */
  1071         -  zLowerName = sqlite3DbStrDup(db, pDef->zName);
  1072         -  if( zLowerName ){
  1073         -    for(z=(unsigned char*)zLowerName; *z; z++){
  1074         -      *z = sqlite3UpperToLower[*z];
         1072  +#ifdef SQLITE_DEBUG
         1073  +  {
         1074  +    int i;
         1075  +    for(i=0; pDef->zName[i]; i++){
         1076  +      unsigned char x = (unsigned char)pDef->zName[i];
         1077  +      assert( x==sqlite3UpperToLower[x] );
  1075   1078       }
  1076         -    rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xSFunc, &pArg);
  1077         -    sqlite3DbFree(db, zLowerName);
  1078   1079     }
         1080  +#endif
         1081  +  rc = pMod->xFindFunction(pVtab, nArg, pDef->zName, &xSFunc, &pArg);
  1079   1082     if( rc==0 ){
  1080   1083       return pDef;
  1081   1084     }
  1082   1085   
  1083   1086     /* Create a new ephemeral function definition for the overloaded
  1084   1087     ** function */
  1085   1088     pNew = sqlite3DbMallocZero(db, sizeof(*pNew)

Changes to test/autoinc.test.

    20     20   # If the library is not compiled with autoincrement support then
    21     21   # skip all tests in this file.
    22     22   #
    23     23   ifcapable {!autoinc} {
    24     24     finish_test
    25     25     return
    26     26   }
           27  +
           28  +if {[permutation]=="inmemory_journal"} {
           29  +  finish_test
           30  +  return
           31  +}
    27     32   
    28     33   sqlite3_db_config_lookaside db 0 0 0
    29     34   
    30     35   # The database is initially empty.
    31     36   #
    32     37   do_test autoinc-1.1 {
    33     38     execsql {
................................................................................
   726    731     db close
   727    732     sqlite3 db test.db
   728    733     set res [catch {db eval {
   729    734       INSERT INTO t1(b) VALUES('two');
   730    735     }} msg]
   731    736     lappend res $msg
   732    737   } {1 {database disk image is malformed}}
          738  +ifcapable vtab {
          739  +  set err "database disk image is malformed"
          740  +} else {
          741  +  set err {malformed database schema (sqlite_sequence) - near "VIRTUAL": syntax error}
          742  +}
   733    743   do_test autoinc-12.3 {
   734    744     db close
   735    745     forcedelete test.db
   736    746     sqlite3 db test.db
   737    747     db eval {
   738    748      CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
   739    749      INSERT INTO t1(b) VALUES('one');
................................................................................
   744    754     }
   745    755     db close
   746    756     sqlite3 db test.db
   747    757     set res [catch {db eval {
   748    758       INSERT INTO t1(b) VALUES('two');
   749    759     }} msg]
   750    760     lappend res $msg
   751         -} {1 {database disk image is malformed}}
          761  +} [list 1 $err]
   752    762   do_test autoinc-12.4 {
   753    763     db close
   754    764     forcedelete test.db
   755    765     sqlite3 db test.db
   756    766     db eval {
   757    767       CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT);
   758    768       INSERT INTO t1(b) VALUES('one');

Changes to test/csv01.test.

   143    143   # 2018-04-24
   144    144   # Memory leak reported on the sqlite-users mailing list by Ralf Junker.
   145    145   #
   146    146   do_catchsql_test 4.3 {
   147    147     CREATE VIRTUAL TABLE IF NOT EXISTS temp.t1
   148    148     USING csv(filename='FileDoesNotExist.csv');
   149    149   } {1 {cannot open 'FileDoesNotExist.csv' for reading}}
          150  +
          151  +# 2018-06-02
          152  +# Problem with single-column CSV support reported on the mailing list
          153  +# by Trent W. Buck.
          154  +#
          155  +do_execsql_test 4.4 {
          156  +  CREATE VIRTUAL TABLE temp.trent USING csv(data='1');
          157  +  SELECT * FROM trent;
          158  +} {1}
   150    159   
   151    160   finish_test

Changes to test/e_fkey.test.

    20     20   # either SQLITE_OMIT_TRIGGER or SQLITE_OMIT_FOREIGN_KEY was defined
    21     21   # at build time).
    22     22   #
    23     23   
    24     24   set testdir [file dirname $argv0]
    25     25   source $testdir/tester.tcl
    26     26   
    27         -proc eqp {sql {db db}} { uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db }
           27  +proc eqp {sql {db db}} { 
           28  +  uplevel [subst -nocommands {
           29  +    set eqpres [list]
           30  +    $db eval "$sql" {
           31  +      lappend eqpres [set detail]
           32  +    }
           33  +    set eqpres
           34  +  }]
           35  +}
           36  +
           37  +proc do_detail_test {tn sql res} {
           38  +  set normalres [list {*}$res]
           39  +  uplevel [subst -nocommands {
           40  +    do_test $tn {
           41  +      eqp { $sql }
           42  +    } {$normalres}
           43  +  }]
           44  +}
    28     45   
    29     46   ###########################################################################
    30     47   ### SECTION 2: Enabling Foreign Key Support
    31     48   ###########################################################################
    32     49   
    33     50   #-------------------------------------------------------------------------
    34     51   # EVIDENCE-OF: R-33710-56344 In order to use foreign key constraints in
................................................................................
   966    983         trackid     INTEGER, 
   967    984         trackname   TEXT, 
   968    985         trackartist INTEGER,
   969    986         FOREIGN KEY(trackartist) REFERENCES artist(artistid)
   970    987       );
   971    988     }
   972    989   } {}
   973         -do_execsql_test e_fkey-25.2 {
          990  +do_detail_test e_fkey-25.2 {
   974    991     PRAGMA foreign_keys = OFF;
   975    992     EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
   976    993     EXPLAIN QUERY PLAN SELECT rowid FROM track WHERE trackartist = ?;
   977    994   } {
   978         -  0 0 0 {SCAN TABLE artist} 
   979         -  0 0 0 {SCAN TABLE track}
          995  +  {SCAN TABLE artist} 
          996  +  {SCAN TABLE track}
   980    997   }
   981         -do_execsql_test e_fkey-25.3 {
          998  +do_detail_test e_fkey-25.3 {
   982    999     PRAGMA foreign_keys = ON;
   983   1000     EXPLAIN QUERY PLAN DELETE FROM artist WHERE 1;
   984   1001   } {
   985         -  0 0 0 {SCAN TABLE artist} 
   986         -  0 0 0 {SCAN TABLE track}
         1002  +  {SCAN TABLE artist} 
         1003  +  {SCAN TABLE track}
   987   1004   }
   988   1005   do_test e_fkey-25.4 {
   989   1006     execsql {
   990   1007       INSERT INTO artist VALUES(5, 'artist 5');
   991   1008       INSERT INTO artist VALUES(6, 'artist 6');
   992   1009       INSERT INTO artist VALUES(7, 'artist 7');
   993   1010       INSERT INTO track VALUES(1, 'track 1', 5);
................................................................................
  1093   1110       );
  1094   1111       CREATE INDEX trackindex ON track(trackartist);
  1095   1112     }
  1096   1113   } {}
  1097   1114   do_test e_fkey-27.2 {
  1098   1115     eqp { INSERT INTO artist VALUES(?, ?) }
  1099   1116   } {}
  1100         -do_execsql_test e_fkey-27.3 {
         1117  +do_detail_test e_fkey-27.3 {
  1101   1118     EXPLAIN QUERY PLAN UPDATE artist SET artistid = ?, artistname = ?
  1102   1119   } {
  1103         -  0 0 0 {SCAN TABLE artist} 
  1104         -  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)} 
  1105         -  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
         1120  +  {SCAN TABLE artist} 
         1121  +  {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)} 
         1122  +  {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1106   1123   }
  1107         -do_execsql_test e_fkey-27.4 {
         1124  +do_detail_test e_fkey-27.4 {
  1108   1125     EXPLAIN QUERY PLAN DELETE FROM artist
  1109   1126   } {
  1110         -  0 0 0 {SCAN TABLE artist} 
  1111         -  0 0 0 {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
         1127  +  {SCAN TABLE artist} 
         1128  +  {SEARCH TABLE track USING COVERING INDEX trackindex (trackartist=?)}
  1112   1129   }
  1113         -
  1114   1130   
  1115   1131   ###########################################################################
  1116   1132   ### SECTION 4.1: Composite Foreign Key Constraints
  1117   1133   ###########################################################################
  1118   1134   
  1119   1135   #-------------------------------------------------------------------------
  1120   1136   # Check that parent and child keys must have the same number of columns.

Changes to test/malloc5.test.

   170    170     db eval {SELECT * FROM abc} {
   171    171       incr nRelease [sqlite3_release_memory]
   172    172       lappend data $a $b $c
   173    173     }
   174    174     execsql {
   175    175       COMMIT;
   176    176     }
   177         -  list $nRelease $data
   178         -} [list $pgalloc [list 1 2 3 4 5 6]]
          177  +  value_in_range $::pgalloc $::mrange $nRelease
          178  +} [value_in_range $::pgalloc $::mrange]
          179  +do_test malloc5-2.2.1 {
          180  +  set data
          181  +} {1 2 3 4 5 6}
   179    182   
   180    183   do_test malloc5-3.1 {
   181    184     # Simple test to show that if two pagers are opened from within this
   182    185     # thread, memory is freed from both when sqlite3_release_memory() is
   183    186     # called.
   184    187     execsql {
   185    188       BEGIN;

Changes to test/misc7.test.

   266    266   
   267    267   db close
   268    268   forcedelete test.db
   269    269   forcedelete test.db-journal
   270    270   sqlite3 db test.db
   271    271   
   272    272   ifcapable explain {
   273         -  do_execsql_test misc7-14.1 {
          273  +  do_execsql_test misc7-14.0 {
   274    274       CREATE TABLE abc(a PRIMARY KEY, b, c);
   275         -    EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE rowid = 1;
   276         -  } {
   277         -    0 0 0 {SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?)}
   278    275     }
   279         -  do_execsql_test misc7-14.2 {
   280         -    EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 WHERE a = 1;
   281         -  } {0 0 0 
   282         -     {SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?)}
   283         -  }
   284         -  do_execsql_test misc7-14.3 {
   285         -    EXPLAIN QUERY PLAN SELECT * FROM abc AS t2 ORDER BY a;
   286         -  } {0 0 0 
   287         -     {SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1}
   288         -  }
          276  +  do_eqp_test misc7-14.1 {
          277  +    SELECT * FROM abc AS t2 WHERE rowid = 1;
          278  +  } {
          279  +  QUERY PLAN
          280  +  `--SEARCH TABLE abc AS t2 USING INTEGER PRIMARY KEY (rowid=?)
          281  +}
          282  +  do_eqp_test misc7-14.2 {
          283  +    SELECT * FROM abc AS t2 WHERE a = 1;
          284  +} {
          285  +  QUERY PLAN
          286  +  `--SEARCH TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1 (a=?)
          287  +}
          288  +  do_eqp_test misc7-14.3 {
          289  +    SELECT * FROM abc AS t2 ORDER BY a;
          290  +  } {
          291  +  QUERY PLAN
          292  +  `--SCAN TABLE abc AS t2 USING INDEX sqlite_autoindex_abc_1
          293  +}
   289    294   }
   290    295   
   291    296   db close
   292    297   forcedelete test.db
   293    298   forcedelete test.db-journal
   294    299   sqlite3 db test.db
   295    300   

Changes to test/oserror.test.

    51     51   # a call to getcwd() may fail if there are no free file descriptors. So
    52     52   # an error may be reported for either open() or getcwd() here.
    53     53   #
    54     54   if {![clang_sanitize_address]} {
    55     55     do_test 1.1.1 {
    56     56       set ::log [list]
    57     57       list [catch {
    58         -      for {set i 0} {$i < 2000} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
           58  +      for {set i 0} {$i < 20000} {incr i} { sqlite3 dbh_$i test.db -readonly 1 }
    59     59       } msg] $msg
    60     60     } {1 {unable to open database file}}
    61     61     do_test 1.1.2 {
    62         -    catch { for {set i 0} {$i < 2000} {incr i} { dbh_$i close } }
           62  +    catch { for {set i 0} {$i < 20000} {incr i} { dbh_$i close } }
    63     63     } {1}
    64     64     do_re_test 1.1.3 { 
    65     65       lindex $::log 0 
    66     66     } {^os_unix.c:\d+: \(\d+\) (open|getcwd)\(.*test.db\) - }
    67     67   }
    68     68   
    69     69   

Changes to test/resetdb.test.

    10     10   #***********************************************************************
    11     11   # Test cases for SQLITE_DBCONFIG_RESET_DATABASE
    12     12   #
    13     13   
    14     14   set testdir [file dirname $argv0]
    15     15   source $testdir/tester.tcl
    16     16   set testprefix resetdb
           17  +
           18  +do_not_use_codec
    17     19   
    18     20   ifcapable !vtab||!compound {
           21  +  finish_test
           22  +  return
           23  +}
           24  +
           25  +# In the "inmemory_journal" permutation, each new connection executes 
           26  +# "PRAGMA journal_mode = memory". This fails with SQLITE_BUSY if attempted
           27  +# on a wal mode database with existing connections. For this and a few
           28  +# other reasons, this test is not run as part of "inmemory_journal".
           29  +#
           30  +# Permutation "journaltest" does not support wal mode.
           31  +#
           32  +if {[permutation]=="inmemory_journal"
           33  + || [permutation]=="journaltest"
           34  +} {
    19     35     finish_test
    20     36     return
    21     37   }
    22     38   
    23     39   # Create a sample database
    24     40   do_execsql_test 100 {
           41  +  PRAGMA auto_vacuum = 0;
    25     42     PRAGMA page_size=4096;
    26     43     CREATE TABLE t1(a,b);
    27     44     WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
    28     45       INSERT INTO t1(a,b) SELECT x, randomblob(300) FROM c;
    29     46     CREATE INDEX t1a ON t1(a);
    30     47     CREATE INDEX t1b ON t1(b);
    31     48     SELECT sum(a), sum(length(b)) FROM t1;
................................................................................
    78     95   # with a different page size and in WAL mode.
    79     96   #
    80     97   db close
    81     98   db2 close
    82     99   forcedelete test.db
    83    100   sqlite3 db test.db
    84    101   do_execsql_test 300 {
          102  +  PRAGMA auto_vacuum = 0;
    85    103     PRAGMA page_size=8192;
    86    104     PRAGMA journal_mode=WAL;
    87    105     CREATE TABLE t1(a,b);
    88    106     WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<20)
    89    107       INSERT INTO t1(a,b) SELECT x, randomblob(1300) FROM c;
    90    108     CREATE INDEX t1a ON t1(a);
    91    109     CREATE INDEX t1b ON t1(b);
................................................................................
   113    131   } {1 {file is not a database}}
   114    132   
   115    133   do_test 330 {
   116    134     catchsql {
   117    135       PRAGMA quick_check
   118    136     } db2
   119    137   } {1 {file is not a database}}
          138  +
          139  +db2 cache flush         ;# Required by permutation "prepare".
   120    140   
   121    141   # Reset the database yet again.  Verify that the page size and
   122    142   # journal mode are preserved.
   123    143   #
   124    144   do_test 400 {
   125    145     sqlite3_db_config db RESET_DB 1
   126    146     db eval VACUUM

Changes to tool/mkopcodeh.tcl.

    71     71     }
    72     72   
    73     73     # Scan for "case OP_aaaa:" lines in the vdbe.c file
    74     74     #
    75     75     if {[regexp {^case OP_} $line]} {
    76     76       set line [split $line]
    77     77       set name [string trim [lindex $line 1] :]
           78  +    if {$name=="OP_Abortable"} continue;  # put OP_Abortable last 
    78     79       set op($name) -1
    79     80       set jump($name) 0
    80     81       set in1($name) 0
    81     82       set in2($name) 0
    82     83       set in3($name) 0
    83     84       set out2($name) 0
    84     85       set out3($name) 0
................................................................................
   109    110     }
   110    111   }
   111    112   
   112    113   # Assign numbers to all opcodes and output the result.
   113    114   #
   114    115   puts "/* Automatically generated.  Do not edit */"
   115    116   puts "/* See the tool/mkopcodeh.tcl script for details */"
   116         -foreach name {OP_Noop OP_Explain} {
          117  +foreach name {OP_Noop OP_Explain OP_Abortable} {
   117    118     set jump($name) 0
   118    119     set in1($name) 0
   119    120     set in2($name) 0
   120    121     set in3($name) 0
   121    122     set out2($name) 0
   122    123     set out3($name) 0
   123    124     set op($name) -1