]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Changes to insure that lookaside memory allocations are never used to hold
authordrh <drh@noemail.net>
Tue, 24 Mar 2009 15:08:09 +0000 (15:08 +0000)
committerdrh <drh@noemail.net>
Tue, 24 Mar 2009 15:08:09 +0000 (15:08 +0000)
schema content.
Ticket #3743. (CVS 6377)

FossilOrigin-Name: ea74d8dc62f5784089aa8ef098e97c505a79b176

19 files changed:
manifest
manifest.uuid
src/alter.c
src/build.c
src/callback.c
src/expr.c
src/malloc.c
src/parse.y
src/prepare.c
src/select.c
src/sqliteInt.h
src/tclsqlite.c
src/tokenize.c
src/vdbeblob.c
src/vtab.c
test/alter.test
test/check.test
test/lookaside.test
test/mutex1.test

index 9833ec7f509999452fd11e90a495bfda4e990caa..30de5671726ffc29a520275ab79816e2f9e625c9 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sa\scomment\sto\sprepare.c\sexplaining\swhy\sthe\slookaside\sbuffer\sis\sdisabled\sbefore\ssqlite3_exec()\sis\scalled\sto\sparse\sa\sschema\sstatement.\sNo\scode\schanges.\s(CVS\s6376)
-D 2009-03-24T04:46:08
+C Changes\sto\sinsure\sthat\slookaside\smemory\sallocations\sare\snever\sused\sto\shold\r\nschema\scontent.\r\nTicket\s#3743.\s(CVS\s6377)
+D 2009-03-24T15:08:10
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 583e87706abc3026960ed759aff6371faf84c211
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -96,7 +96,7 @@ F spec.template 86a4a43b99ebb3e75e6b9a735d5fd293a24e90ca
 F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b
 F sqlite3.1 6be1ad09113570e1fc8dcaff84c9b0b337db5ffc
 F sqlite3.pc.in ae6f59a76e862f5c561eb32a380228a02afc3cad
-F src/alter.c b95815ccc92477b1b5874fb0fe20f65c867e9cc8
+F src/alter.c 472c0a4733de40f1d997ff4fd746042b707faf7b
 F src/analyze.c 3585d1a4c480ee85b65cf0a676e05d2c29eb6bdb
 F src/attach.c d34589d5c85d81e755e4a8fc946d313915a6fa6d
 F src/auth.c c8b2ab5c8bad4bd90ed7c294694f48269162c627
@@ -106,12 +106,12 @@ F src/btmutex.c 341502bc496dc0840dcb00cde65680fb0e85c3ab
 F src/btree.c 1a709b6aac17e520de49b6cbad5cc14000910d8b
 F src/btree.h e302c5747494067cd4f5763000fbe7bca767d816
 F src/btreeInt.h df64030d632f8c8ac217ed52e8b6b3eacacb33a5
-F src/build.c f217f9ce2528c5b968f77a11998948466ea81bc9
-F src/callback.c 09c6fedc77a45db99ba25a75d61382830314b357
+F src/build.c be2bdaf2a3f6b24ef08dc14b9fd33aa84c689908
+F src/callback.c 73016376d6848ba987709e8c9048d4f0e0776036
 F src/complete.c cb14e06dbe79dee031031f0d9e686ff306afe07c
 F src/date.c 0d804df3bbda46329946a01ff5c75c3f4f135218
 F src/delete.c eb1066b2f35489fee46ad765d2b66386fc7d8adf
-F src/expr.c ca5d176acc69497401cc5c376a91f785159b83a4
+F src/expr.c 614a60615fd4bd186459c0c1fa87fdefedb76c26
 F src/fault.c dc88c821842157460750d2d61a8a8b4197d047ff
 F src/func.c de2eed7d96365210faecda877c5a12cf440bdf42
 F src/global.c 448419c44ce0701104c2121b0e06919b44514c0c
@@ -123,7 +123,7 @@ F src/journal.c e00df0c0da8413ab6e1bb7d7cab5665d4a9000d0
 F src/legacy.c 2ad5b52df322d0f132f66817095e0e79c8942611
 F src/loadext.c 3f96631089fc4f3871a67f02f2e4fc7ea4d51edc
 F src/main.c 95e13cd23b7a88e33c1acfe233c748fd9dd7e467
-F src/malloc.c 1d862da7eb382d4b79b55436b04278fedec288e7
+F src/malloc.c a1f0f8ae110abb8eb546e259ab0eaea7e0f9b588
 F src/mem0.c f2f84062d1f35814d6535c9f9e33de3bfb3b132c
 F src/mem1.c e6d5c23941288df8191b8a98c28e3f57771e2270
 F src/mem2.c d02bd6a5b34f2d59012a852615621939d9c09548
@@ -145,25 +145,25 @@ F src/os_unix.c a20fe61c025117452c003ae4cafd05c3e610b616
 F src/os_win.c 40636702058ed4dcd35d68151bfab56d4997cdc1
 F src/pager.c da7ffd068347cdc88b477875585d53dcf2bdc4a7
 F src/pager.h 0c9f3520c00d8a3b8e792ca56c9a11b6b02b4b0f
-F src/parse.y 30ae25cd9ce8002527fca3011052a7fba0aff349
+F src/parse.y b9ba0946a13e9f32a96044e64a3e8780269b08b0
 F src/pcache.c fcf7738c83c4d3e9d45836b2334c8a368cc41274
 F src/pcache.h 9b927ccc5a538e31b4c3bc7eec4f976db42a1324
 F src/pcache1.c f587565f4ba0fd1772067eaa96814dce761b7a4c
 F src/pragma.c 22ed04836aab8ce134c53be1ca896f3ad20fabdb
-F src/prepare.c 662b7d30574558822ca6f0621cbb7aef7e87cee3
+F src/prepare.c 157a7528ba686964ab0e0687749ca30adb9ab614
 F src/printf.c 9866a9a9c4a90f6d4147407f373df3fd5d5f9b6f
 F src/random.c 676b9d7ac820fe81e6fb2394ac8c10cff7f38628
 F src/resolve.c 094e44450371fb27869eb8bf679aacbe51fdc56d
 F src/rowset.c ba9375f37053d422dd76965a9c370a13b6e1aac4
-F src/select.c 4d0b77fd76ff80f09a798ee98953e344c9de8fbb
+F src/select.c 607e5b2d3be379781e5be3ac0854f5c237152d26
 F src/shell.c 0a11f831603f17fea20ca97133c0f64e716af4a7
 F src/sqlite.h.in 0db1e59d89aeacb8fe64a19fd14c13a796060ccb
 F src/sqlite3ext.h 1db7d63ab5de4b3e6b83dd03d1a4e64fef6d2a17
-F src/sqliteInt.h b6858d742b30d556886cca16437fb0a4aad2e065
+F src/sqliteInt.h ddaadadd35b07e468b0a07a5575c796a71e89dc1
 F src/sqliteLimit.h ffe93f5a0c4e7bd13e70cd7bf84cfb5c3465f45d
 F src/status.c 237b193efae0cf6ac3f0817a208de6c6c6ef6d76
 F src/table.c 332ab0ea691e63862e2a8bdfe2c0617ee61062a3
-F src/tclsqlite.c 8a472804b901d4559213eeda538c2eadb2ad7f2a
+F src/tclsqlite.c 3d64c59fa441c4b6d550cb97b03c91693b87bbd1
 F src/test1.c 17300af44640eea439778f5b5e03e0d68a6f00a2
 F src/test2.c 71c22e2974f8094fe0fd1eba8f27872dde9b2a39
 F src/test3.c d3115b301c6ee761b102f315fe24125f3d6c3a4d
@@ -194,7 +194,7 @@ F src/test_server.c f0a403b5f699c09bd2b1236b6f69830fd6221f6b
 F src/test_tclvar.c 9e42fa59d3d2f064b7ab8628e7ab2dc8a9fe93d4
 F src/test_thread.c 1c7a895dbc469d87d22c1b36f269a6f799fc47a8
 F src/test_wsd.c 3ae5101de6cbfda2720152ab659ea84079719241
-F src/tokenize.c 6987fb7f0d6a87ac53499aee568cabb05eb4bea8
+F src/tokenize.c fff72a9bc0af326583e5966daecebeb5ed9b9614
 F src/trigger.c 21f39db410cdc32166a94900ac1b3df98ea560e6
 F src/update.c 8ededddcde6f7b6da981dd0429a5d34518a475b7
 F src/utf.c 1da9c832dba0fa8f865b5b902d93f420a1ee4245
@@ -205,15 +205,15 @@ F src/vdbe.h d70a68bee196ab228914a3902c79dbd24342a0f2
 F src/vdbeInt.h 53a2f4696871712646c77351904576cca6ad9752
 F src/vdbeapi.c 025d83f51f5e0a6e0fb55d603103d24825577967
 F src/vdbeaux.c be958dfe3a476ccb58dbc0d5786d58717f40f4b0
-F src/vdbeblob.c 3408f5e1ab62627300b9c9c3168cff1de65e1658
+F src/vdbeblob.c e67757450ae8581a8b354d9d7e467e41502dfe38
 F src/vdbemem.c 38615b5d4b1b3b5a1221a5623578e5e3864e4888
-F src/vtab.c 18d671f0a45d0159088070bc6b219a3105034c87
+F src/vtab.c f1aba5a6dc1f83b97a39fbbc58ff8cbc76311347
 F src/walker.c 42bd3f00ca2ef5ae842304ec0d59903ef051412d
 F src/where.c 55b13048fd0f82b330511e37766208b96fd764df
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/alias.test 597662c5d777a122f9a3df0047ea5c5bd383a911
 F test/all.test 14165b3e32715b700b5f0cbf8f6e3833dda0be45
-F test/alter.test 958b36b7e9752e8a4ed03ba34690b06e48b0e3e0
+F test/alter.test 645b2e8d23c9936f9494af9d2fa7f8351a248c6e
 F test/alter2.test dd55146e812622c8fc51fd2216bcd8dca8880752
 F test/alter3.test 25b95a136708f22b87184fa6a4309eea03d65153
 F test/alter4.test 9386ffd1e9c7245f43eca412b2058d747509cc1f
@@ -261,7 +261,7 @@ F test/capi3b.test 664eb55318132f292f2c436f90906f578cad6b97
 F test/capi3c.test ce0fcbbaccfc9703fb247ea9ac5ada96dc9ca047
 F test/capi3d.test 57d83b690d7364bde02cddbf8339a4b50d80ce23
 F test/cast.test 166951664a0b0a2e0f8fb5997a152490c6363932
-F test/check.test 024ed399600b799160378cf9d9f436bdf5dfd184
+F test/check.test 4f245970b4583555a349dceacdae7976222d834a
 F test/collate1.test e3eaa48c21e150814be1a7b852d2a8af24458d04
 F test/collate2.test 04cebe4a033be319d6ddbb3bbc69464e01700b49
 F test/collate3.test d28d2cfab2c3a3d4628ae4b2b7afc9965daa3b4c
@@ -435,7 +435,7 @@ F test/lock3.test 8adfbf438b96316267611214d494ebc1311b8cda
 F test/lock4.test 991b57669a868bbfd39fe0c0df0d493985829105
 F test/lock5.test 6b1f78f09ad1522843dad571b76b321e6f439bf7
 F test/lock6.test 862aa71e97b288d6b3f92ba3313f51bd0b003776
-F test/lookaside.test e69f822f13745f1d5c445c6e30e30f059f30c8e5
+F test/lookaside.test b3e928efeb9a31147bb72350462831e104ec2c1e
 F test/main.test 187a9a1b5248ed74a83838c581c15ec6023b555b
 F test/make-where7.tcl 40bb740b37eead343eaf57b74ab72d2a5a304745
 F test/malloc.test 7388109bccc42888ad4e2cb01b2169eb98d4d283
@@ -475,7 +475,7 @@ F test/misc5.test 6a5c1e3217a95b0db05ff9a0f1ecb5ce9043ffef
 F test/misc6.test 953cc693924d88e6117aeba16f46f0bf5abede91
 F test/misc7.test debcd7dbfd847ae97b16ceb7fec6f567902874a6
 F test/misuse.test 30b3a458e5a70c31e74c291937b6c82204c59f33
-F test/mutex1.test ace3ba551ef0cd864df2b820b5dbe18d28b4db6c
+F test/mutex1.test ebd54720401fafe854799dc86b7bf60b75631935
 F test/mutex2.test bfeaeac2e73095b2ac32285d2756e3a65e681660
 F test/nan.test c627d79b3d36ea892563fd67584b3e8a18f0618a
 F test/notify1.test 9a985a94f34de1b24daf25fd86b6d5033ba532d0
@@ -709,7 +709,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P 7c2df04b52a40d5d1b744ad1097f7c12143d8c2c
-R 703f5e83ff72a6ceec997ed32c6b6e4e
-U danielk1977
-Z 3f57c912d20fa971468a1ff4756c679d
+P 8ca6a665650c9683a202f3ced17b14f7c85624bf
+R 8c93b31b69e1e1ff0ee86ef0261f4c0c
+U drh
+Z 226c5008143d3b67d9a3ebdf1903227a
index ba6eb57342f8315ebb187f012773ad900d19be82..fba0d0c964080b1318f8ec7fc29d438246e3f1dd 100644 (file)
@@ -1 +1 @@
-8ca6a665650c9683a202f3ced17b14f7c85624bf
\ No newline at end of file
+ea74d8dc62f5784089aa8ef098e97c505a79b176
\ No newline at end of file
index 2a1b18ea16fc743452102f55b689c51e531b97ee..d89b4339b6389223add304d51d8349e24144d17f 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that used to generate VDBE code
 ** that implements the ALTER TABLE command.
 **
-** $Id: alter.c,v 1.54 2009/02/28 10:47:42 danielk1977 Exp $
+** $Id: alter.c,v 1.55 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -593,7 +593,7 @@ void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
   if( !pNew ) goto exit_begin_add_column;
   pParse->pNewTable = pNew;
   pNew->nRef = 1;
-  pNew->db = db;
+  pNew->dbMem = pTab->dbMem;
   pNew->nCol = pTab->nCol;
   assert( pNew->nCol>0 );
   nAlloc = (((pNew->nCol-1)/8)*8)+8;
index e9fcb27b6697536b3da7cf2beb634f66ab579148..f226334dd655eaa338be4520bc7623913b2655bd 100644 (file)
@@ -22,7 +22,7 @@
 **     COMMIT
 **     ROLLBACK
 **
-** $Id: build.c,v 1.525 2009/03/21 16:19:26 drh Exp $
+** $Id: build.c,v 1.526 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -339,7 +339,7 @@ Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
 ** Reclaim the memory used by an index
 */
 static void freeIndex(Index *p){
-  sqlite3 *db = p->pTable->db;
+  sqlite3 *db = p->pTable->dbMem;
   sqlite3DbFree(db, p->zColAff);
   sqlite3DbFree(db, p);
 }
@@ -467,7 +467,7 @@ void sqlite3CommitInternalChanges(sqlite3 *db){
 static void sqliteResetColumnNames(Table *pTable){
   int i;
   Column *pCol;
-  sqlite3 *db = pTable->db;
+  sqlite3 *db = pTable->dbMem;
   assert( pTable!=0 );
   if( (pCol = pTable->aCol)!=0 ){
     for(i=0; i<pTable->nCol; i++, pCol++){
@@ -498,7 +498,7 @@ void sqlite3DeleteTable(Table *pTable){
   sqlite3 *db;
 
   if( pTable==0 ) return;
-  db = pTable->db;
+  db = pTable->dbMem;
 
   /* Do not delete the table until the reference count reaches zero. */
   pTable->nRef--;
@@ -836,7 +836,7 @@ void sqlite3StartTable(
   pTable->iPKey = -1;
   pTable->pSchema = db->aDb[iDb].pSchema;
   pTable->nRef = 1;
-  pTable->db = db;
+  pTable->dbMem = db->lookaside.bEnabled ? db : 0;
   if( pParse->pNewTable ) sqlite3DeleteTable(pParse->pNewTable);
   pParse->pNewTable = pTable;
 
@@ -1855,9 +1855,11 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
   assert( pTable->pSelect );
   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
   if( pSel ){
+    int enableLookaside = db->lookaside.bEnabled;
     n = pParse->nTab;
     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
     pTable->nCol = -1;
+    db->lookaside.bEnabled = 0;
 #ifndef SQLITE_OMIT_AUTHORIZATION
     xAuth = db->xAuth;
     db->xAuth = 0;
@@ -1866,6 +1868,7 @@ int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
 #else
     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
 #endif
+    db->lookaside.bEnabled = enableLookaside;
     pParse->nTab = n;
     if( pSelTab ){
       assert( pTable->aCol==0 );
index ca41c08f427ae702c57694f0909041fe6b5fc888..c4a5280d271af08c3b158c7cad6ef410ec81f10d 100644 (file)
@@ -13,7 +13,7 @@
 ** This file contains functions used to access the internal hash tables
 ** of user defined functions and collation sequences.
 **
-** $Id: callback.c,v 1.36 2009/03/05 04:20:32 shane Exp $
+** $Id: callback.c,v 1.37 2009/03/24 15:08:10 drh Exp $
 */
 
 #include "sqliteInt.h"
@@ -423,6 +423,7 @@ void sqlite3SchemaFree(void *p){
   sqlite3HashInit(&pSchema->tblHash, 0);
   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
     Table *pTab = sqliteHashData(pElem);
+    assert( pTab->dbMem==0 );
     sqlite3DeleteTable(pTab);
   }
   sqlite3HashClear(&temp1);
index edea9224d8d3275413299ac07d753572c53b4cc0..01e151e831f5dca90590248d9b6c469a481b3682 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains routines used for analyzing expressions and
 ** for generating VDBE code that evaluates expressions in SQLite.
 **
-** $Id: expr.c,v 1.421 2009/03/23 04:33:32 danielk1977 Exp $
+** $Id: expr.c,v 1.422 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -404,8 +404,22 @@ Expr *sqlite3Expr(
   pNew->iAgg = -1;
   pNew->span.z = (u8*)"";
   if( pToken ){
+    int c;
     assert( pToken->dyn==0 );
-    pNew->span = pNew->token = *pToken;
+    pNew->span = *pToken;
+    /* The pToken->z value is constant and must not change.  But
+    ** this expression might be passed to sqlite3DequoteExpr() which
+    ** will attempt to modify pNew->token.z.  Hence, if the token
+    ** is quoted, make a copy now so that DequoteExpr() will change
+    ** the copy rather than the original (read-only) text.
+    */
+    if( pToken->n>=2 
+         && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
+      sqlite3TokenCopy(db, &pNew->token, pToken);
+    }else{
+      pNew->token = *pToken;
+    }
   }else if( pLeft ){
     if( pRight ){
       if( pRight->span.dyn==0 && pLeft->span.dyn==0 ){
@@ -511,16 +525,15 @@ Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
   assert( pToken );
   pNew = sqlite3DbMallocZero(db, sizeof(Expr) );
   if( pNew==0 ){
-    sqlite3ExprListDelete(db, pList); /* Avoid leaking memory when malloc fails */
+    sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
     return 0;
   }
   pNew->op = TK_FUNCTION;
   pNew->x.pList = pList;
   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
   assert( pToken->dyn==0 );
-  pNew->token = *pToken;
-  pNew->span = pNew->token;
-
+  pNew->span = *pToken;
+  sqlite3TokenCopy(db, &pNew->token, pToken);
   sqlite3ExprSetHeight(pParse, pNew);
   return pNew;
 }
@@ -645,27 +658,10 @@ void sqlite3ExprDelete(sqlite3 *db, Expr *p){
 ** If so, remove the quotation marks.
 */
 void sqlite3DequoteExpr(sqlite3 *db, Expr *p){
-  if( ExprHasAnyProperty(p, EP_Dequoted) ){
-    return;
+  if( !ExprHasAnyProperty(p, EP_Dequoted) ){
+    ExprSetProperty(p, EP_Dequoted);
+    sqlite3Dequote((char*)p->token.z);
   }
-  ExprSetProperty(p, EP_Dequoted);
-
-  /* If p->token.dyn==0 and none of EP_Reduced, EP_TokenOnly, or
-  ** EP_SpanOnly are set, that means that the p->token.z string points
-  ** back to the original SQL statement text.  In that case, we need
-  ** to make a copy before modifying the string, otherwise we would
-  ** corrupt the original SQL statement text.
-  */
-  testcase( p->token.dyn==0 && ExprHasProperty(p, EP_Reduced) );
-  testcase( p->token.dyn==0 && ExprHasProperty(p, EP_TokenOnly) );
-  testcase( p->token.dyn==0 && ExprHasProperty(p, EP_SpanOnly) );
-  if( p->token.dyn==0
-   && !ExprHasAnyProperty(p, EP_Reduced|EP_TokenOnly|EP_SpanOnly) 
-  ){
-    sqlite3TokenCopy(db, &p->token, &p->token);
-  }
-
-  sqlite3Dequote((char*)p->token.z);
 }
 
 /*
@@ -876,7 +872,7 @@ static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
 Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
   return exprDup(db, p, flags, 0);
 }
-void sqlite3TokenCopy(sqlite3 *db, Token *pTo, Token *pFrom){
+void sqlite3TokenCopy(sqlite3 *db, Token *pTo, const Token *pFrom){
   if( pTo->dyn ) sqlite3DbFree(db, (char*)pTo->z);
   if( pFrom->z ){
     pTo->n = pFrom->n;
index a995e2c0fb17f0e151252269499134df13ff2404..92e56394e70036c091a658b73fd13b41a12a4cdb 100644 (file)
@@ -12,7 +12,7 @@
 **
 ** Memory allocation functions used throughout sqlite.
 **
-** $Id: malloc.c,v 1.60 2009/03/23 17:49:15 drh Exp $
+** $Id: malloc.c,v 1.61 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 #include <stdarg.h>
@@ -565,10 +565,10 @@ void *sqlite3DbMallocZero(sqlite3 *db, int n){
 */
 void *sqlite3DbMallocRaw(sqlite3 *db, int n){
   void *p;
+  assert( db==0 || sqlite3_mutex_held(db->mutex) );
 #ifndef SQLITE_OMIT_LOOKASIDE
   if( db ){
     LookasideSlot *pBuf;
-    assert( sqlite3_mutex_held(db->mutex) );
     if( db->mallocFailed ){
       return 0;
     }
@@ -600,6 +600,7 @@ void *sqlite3DbMallocRaw(sqlite3 *db, int n){
 */
 void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
   void *pNew = 0;
+  assert( db!=0 );
   assert( sqlite3_mutex_held(db->mutex) );
   if( db->mallocFailed==0 ){
     if( p==0 ){
index 63c68ac68931e89b945000d15776f2b30eaee805..1f14b41682ee0f6ebbc93273421488a69e90ec31 100644 (file)
@@ -14,7 +14,7 @@
 ** the parser.  Lemon will also generate a header file containing
 ** numeric codes for all of the tokens.
 **
-** @(#) $Id: parse.y,v 1.271 2009/03/22 20:36:19 drh Exp $
+** @(#) $Id: parse.y,v 1.272 2009/03/24 15:08:10 drh Exp $
 */
 
 // All token codes are small integers with #defines that begin with "TK_"
@@ -133,9 +133,13 @@ cmd ::= ROLLBACK trans_opt TO savepoint_opt nm(X). {
 ///////////////////// The CREATE TABLE statement ////////////////////////////
 //
 cmd ::= create_table create_table_args.
-create_table ::= CREATE temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
+create_table ::= createkw temp(T) TABLE ifnotexists(E) nm(Y) dbnm(Z). {
    sqlite3StartTable(pParse,&Y,&Z,T,0,0,E);
 }
+createkw(A) ::= CREATE(X).  {
+  pParse->db->lookaside.bEnabled = 0;
+  A = X;
+}
 %type ifnotexists {int}
 ifnotexists(A) ::= .              {A = 0;}
 ifnotexists(A) ::= IF NOT EXISTS. {A = 1;}
@@ -365,7 +369,7 @@ ifexists(A) ::= .            {A = 0;}
 ///////////////////// The CREATE VIEW statement /////////////////////////////
 //
 %ifndef SQLITE_OMIT_VIEW
-cmd ::= CREATE(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). {
+cmd ::= createkw(X) temp(T) VIEW ifnotexists(E) nm(Y) dbnm(Z) AS select(S). {
   sqlite3CreateView(pParse, &X, &Y, &Z, S, T, E);
 }
 cmd ::= DROP VIEW ifexists(E) fullname(X). {
@@ -941,7 +945,7 @@ nexprlist(A) ::= expr(Y).
 
 ///////////////////////////// The CREATE INDEX command ///////////////////////
 //
-cmd ::= CREATE(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
+cmd ::= createkw(S) uniqueflag(U) INDEX ifnotexists(NE) nm(X) dbnm(D)
         ON nm(Y) LP idxlist(Z) RP(E). {
   sqlite3CreateIndex(pParse, &X, &D, 
                      sqlite3SrcListAppend(pParse->db,0,&Y,0), Z, U,
@@ -1024,7 +1028,7 @@ plus_opt ::= .
 
 %ifndef SQLITE_OMIT_TRIGGER
 
-cmd ::= CREATE trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
+cmd ::= createkw trigger_decl(A) BEGIN trigger_cmd_list(S) END(Z). {
   Token all;
   all.z = A.z;
   all.n = (int)(Z.z - A.z) + Z.n;
@@ -1170,6 +1174,7 @@ cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column(Y). {
   sqlite3AlterFinishAddColumn(pParse, &Y);
 }
 add_column_fullname ::= fullname(X). {
+  pParse->db->lookaside.bEnabled = 0;
   sqlite3AlterBeginAddColumn(pParse, X);
 }
 kwcolumn_opt ::= .
@@ -1180,7 +1185,7 @@ kwcolumn_opt ::= COLUMNKW.
 %ifndef SQLITE_OMIT_VIRTUALTABLE
 cmd ::= create_vtab.                       {sqlite3VtabFinishParse(pParse,0);}
 cmd ::= create_vtab LP vtabarglist RP(X).  {sqlite3VtabFinishParse(pParse,&X);}
-create_vtab ::= CREATE VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
+create_vtab ::= createkw VIRTUAL TABLE nm(X) dbnm(Y) USING nm(Z). {
     sqlite3VtabBeginParse(pParse, &X, &Y, &Z);
 }
 vtabarglist ::= vtabarg.
index 1f585381725660acb5c8d5bd5bdb09d8c173d901..dc576959fa5d3a9c5eb9f6095cb3a1074d0d48ba 100644 (file)
@@ -13,7 +13,7 @@
 ** interface, and routines that contribute to loading the database schema
 ** from disk.
 **
-** $Id: prepare.c,v 1.113 2009/03/24 04:46:08 danielk1977 Exp $
+** $Id: prepare.c,v 1.114 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -74,33 +74,14 @@ int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
     ** But because db->init.busy is set to 1, no VDBE code is generated
     ** or executed.  All the parser does is build the internal data
     ** structures that describe the table, index, or view.
-    **
-    ** While the CREATE XXX statement is being processed, the lookaside 
-    ** buffer is disabled. One reason for this is that when running in
-    ** shared cache mode, the objects allocated for the in-memory schema
-    ** might be freed from within a call made on a different database
-    ** connection to db (e.g. if the second connection executes a DROP
-    ** TABLE statement). If the objects that make up the Table structure
-    ** are allocated from within db's lookaside buffer, then db->mutex
-    ** would have to be obtained before they could be freed. This would
-    ** open the door to deadlock in a multi-threaded application.
-    **
-    ** Another reason to disable the lookaside buffer is that lookaside
-    ** gives the greatest performance boost when it is used to allocate
-    ** and free small transient objects. The schema objects, which tend
-    ** to have long lifetimes, are better allocated from the heap.
     */
     char *zErr;
     int rc;
-    u8 lookasideEnabled;
     assert( db->init.busy );
     db->init.iDb = iDb;
     db->init.newTnum = atoi(argv[1]);
-    lookasideEnabled = db->lookaside.bEnabled;
-    db->lookaside.bEnabled = 0;
     rc = sqlite3_exec(db, argv[2], 0, 0, &zErr);
     db->init.iDb = 0;
-    db->lookaside.bEnabled = lookasideEnabled;
     assert( rc!=SQLITE_OK || zErr==0 );
     if( SQLITE_OK!=rc ){
       pData->rc = rc;
index 6c5c232e7793d03d02126facc7675e520c17b242..86c0c8c9f0e77f40e48f33b1d5a48da38dcefd5e 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.504 2009/02/25 08:56:47 danielk1977 Exp $
+** $Id: select.c,v 1.505 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -1284,7 +1284,7 @@ Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
   if( pTab==0 ){
     return 0;
   }
-  pTab->db = db;
+  pTab->dbMem = db->lookaside.bEnabled ? db : 0;
   pTab->nRef = 1;
   pTab->zName = 0;
   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
@@ -3080,7 +3080,7 @@ static int selectExpander(Walker *pWalker, Select *p){
       sqlite3WalkSelect(pWalker, pSel);
       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
       if( pTab==0 ) return WRC_Abort;
-      pTab->db = db;
+      pTab->dbMem = db->lookaside.bEnabled ? db : 0;
       pTab->nRef = 1;
       pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab);
       while( pSel->pPrior ){ pSel = pSel->pPrior; }
index 67836d1b075f558a7d090acbc5b34f1dfeebb6ba..bb79aaa7c2dec14cfb0d4bb56d39622dd4a58425 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** Internal interface definitions for SQLite.
 **
-** @(#) $Id: sqliteInt.h,v 1.845 2009/03/23 04:33:33 danielk1977 Exp $
+** @(#) $Id: sqliteInt.h,v 1.846 2009/03/24 15:08:10 drh Exp $
 */
 #ifndef _SQLITEINT_H_
 #define _SQLITEINT_H_
@@ -686,10 +686,17 @@ struct Schema {
 ** lookaside malloc subsystem.  Each available memory allocation in
 ** the lookaside subsystem is stored on a linked list of LookasideSlot
 ** objects.
+**
+** Lookaside allocations are only allowed for objects that are associated
+** with a particular database connection.  Hence, schema information cannot
+** be stored in lookaside because in shared cache mode the schema information
+** is shared by multiple database connections.  Therefore, while parsing
+** schema information, the Lookaside.bEnabled flag is cleared so that
+** lookaside allocations are not used to construct the schema objects.
 */
 struct Lookaside {
   u16 sz;                 /* Size of each buffer in bytes */
-  u8 bEnabled;            /* True if use lookaside.  False to ignore it */
+  u8 bEnabled;            /* False to disable new lookaside allocations */
   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
   int nOut;               /* Number of buffers currently checked out */
   int mxOut;              /* Highwater mark for nOut */
@@ -1105,7 +1112,7 @@ struct CollSeq {
 ** of a SELECT statement.
 */
 struct Table {
-  sqlite3 *db;         /* Associated database connection.  Might be NULL. */
+  sqlite3 *dbMem;      /* DB connection used for lookaside allocations. */
   char *zName;         /* Name of the table or view */
   int iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
   int nCol;            /* Number of columns in this table */
@@ -2466,7 +2473,7 @@ void sqlite3CompleteInsertion(Parse*, Table*, int, int, int*, int, int, int);
 int sqlite3OpenTableAndIndices(Parse*, Table*, int, int);
 void sqlite3BeginWriteOperation(Parse*, int, int);
 Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
-void sqlite3TokenCopy(sqlite3*,Token*, Token*);
+void sqlite3TokenCopy(sqlite3*,Token*,const Token*);
 ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
 SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
 IdList *sqlite3IdListDup(sqlite3*,IdList*);
index a429662d89f0eadf63e562ecf21408c08498a049..8959e607418e23e770eb124a07ada2f6246af5f2 100644 (file)
@@ -12,7 +12,7 @@
 ** A TCL Interface to SQLite.  Append this file to sqlite3.c and
 ** compile the whole thing to build a TCL-enabled version of SQLite.
 **
-** $Id: tclsqlite.c,v 1.238 2009/03/16 13:19:36 danielk1977 Exp $
+** $Id: tclsqlite.c,v 1.239 2009/03/24 15:08:10 drh Exp $
 */
 #include "tcl.h"
 #include <errno.h>
@@ -2593,8 +2593,21 @@ static int DbMain(void *cd, Tcl_Interp *interp, int objc,Tcl_Obj *const*objv){
   int i;
   const char *zFile;
   const char *zVfs = 0;
-  int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
+  int flags;
   Tcl_DString translatedFilename;
+
+  /* In normal use, each TCL interpreter runs in a single thread.  So
+  ** by default, we can turn of mutexing on SQLite database connections.
+  ** However, for testing purposes it is useful to have mutexes turned
+  ** on.  So, by default, mutexes default off.  But if compiled with
+  ** SQLITE_TCL_DEFAULT_FULLMUTEX then mutexes default on.
+  */
+#ifdef SQLITE_TCL_DEFAULT_FULLMUTEX
+  flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX;
+#else
+  flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX;
+#endif
+
   if( objc==2 ){
     zArg = Tcl_GetStringFromObj(objv[1], 0);
     if( strcmp(zArg,"-version")==0 ){
index afc3dd639a368f058d1b077270f0fdea2d53d239..60313aef435ded611004ac4e3fbb087a4f33a215 100644 (file)
@@ -15,7 +15,7 @@
 ** individual tokens and sends those tokens one-by-one over to the
 ** parser for analysis.
 **
-** $Id: tokenize.c,v 1.153 2009/01/20 16:53:41 danielk1977 Exp $
+** $Id: tokenize.c,v 1.154 2009/03/24 15:08:10 drh Exp $
 */
 #include "sqliteInt.h"
 #include <stdlib.h>
@@ -381,14 +381,17 @@ int sqlite3GetToken(const unsigned char *z, int *tokenType){
 ** error message.
 */
 int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
-  int nErr = 0;
-  int i;
-  void *pEngine;
-  int tokenType;
-  int lastTokenParsed = -1;
-  sqlite3 *db = pParse->db;
-  int mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
+  int nErr = 0;                   /* Number of errors encountered */
+  int i;                          /* Loop counter */
+  void *pEngine;                  /* The LEMON-generated LALR(1) parser */
+  int tokenType;                  /* type of the next token */
+  int lastTokenParsed = -1;       /* type of the previous token */
+  int enableLookaside;            /* Saved value of db->lookaside.bEnabled */
+  sqlite3 *db = pParse->db;       /* The database connection */
+  int mxSqlLen;                   /* Max length of an SQL string */
 
+
+  mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
   if( db->activeVdbeCnt==0 ){
     db->u1.isInterrupted = 0;
   }
@@ -408,6 +411,8 @@ int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
   assert( pParse->nVarExpr==0 );
   assert( pParse->nVarExprAlloc==0 );
   assert( pParse->apVarExpr==0 );
+  enableLookaside = db->lookaside.bEnabled;
+  if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
   while( !db->mallocFailed && zSql[i]!=0 ){
     assert( i>=0 );
     pParse->sLastToken.z = (u8*)&zSql[i];
@@ -462,6 +467,7 @@ abort_parse:
   );
 #endif /* YYDEBUG */
   sqlite3ParserFree(pEngine, sqlite3_free);
+  db->lookaside.bEnabled = enableLookaside;
   if( db->mallocFailed ){
     pParse->rc = SQLITE_NOMEM;
   }
index 82844675ba5bfcdff4c16d3517feb717a6de1f85..de051e1eee4a0f39240f720207e1d0cb0e116416 100644 (file)
@@ -12,7 +12,7 @@
 **
 ** This file contains code used to implement incremental BLOB I/O.
 **
-** $Id: vdbeblob.c,v 1.30 2009/03/19 18:51:07 danielk1977 Exp $
+** $Id: vdbeblob.c,v 1.31 2009/03/24 15:08:10 drh Exp $
 */
 
 #include "sqliteInt.h"
@@ -264,9 +264,13 @@ blob_open_out:
 int sqlite3_blob_close(sqlite3_blob *pBlob){
   Incrblob *p = (Incrblob *)pBlob;
   int rc;
+  sqlite3 *db;
 
+  db = p->db;
+  sqlite3_mutex_enter(db->mutex);
   rc = sqlite3_finalize(p->pStmt);
-  sqlite3DbFree(p->db, p);
+  sqlite3DbFree(db, p);
+  sqlite3_mutex_leave(db->mutex);
   return rc;
 }
 
index a5865d8930afbf31ab4adc7efbc825d824b9ddc1..bfa88e9db2adc68a482752e57208cf7d41544968 100644 (file)
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code used to help implement virtual tables.
 **
-** $Id: vtab.c,v 1.83 2009/03/19 18:51:07 danielk1977 Exp $
+** $Id: vtab.c,v 1.84 2009/03/24 15:08:10 drh Exp $
 */
 #ifndef SQLITE_OMIT_VIRTUALTABLE
 #include "sqliteInt.h"
@@ -118,7 +118,8 @@ void sqlite3VtabUnlock(sqlite3 *db, sqlite3_vtab *pVtab){
 */
 void sqlite3VtabClear(Table *p){
   sqlite3_vtab *pVtab = p->pVtab;
-  sqlite3 *db = p->db;
+  Schema *pSchema = p->pSchema;
+  sqlite3 *db = pSchema ? pSchema->db : 0;
   if( pVtab ){
     assert( p->pMod && p->pMod->pModule );
     sqlite3VtabUnlock(db, pVtab);
index 2f661082871a133d1494eb0e57c8803eab06602b..2db82dd4293f9844c037abf0b91c22e8c7151f64 100644 (file)
@@ -11,7 +11,7 @@
 # This file implements regression tests for SQLite library.  The
 # focus of this script is testing the ALTER TABLE statement.
 #
-# $Id: alter.test,v 1.31 2009/02/13 03:43:32 drh Exp $
+# $Id: alter.test,v 1.32 2009/03/24 15:08:10 drh Exp $
 #
 
 set testdir [file dirname $argv0]
@@ -677,7 +677,7 @@ do_test alter-9.2 {
   execsql {
     SELECT SQLITE_RENAME_TABLE(0,0);
     SELECT SQLITE_RENAME_TABLE(10,20);
-    SELECT SQLITE_RENAME_TABLE("foo", "foo");
+    SELECT SQLITE_RENAME_TABLE('foo', 'foo');
   }
 } {{} {} {}}
 
index cdf2b4617309b9e94aceaf8c73346c0f0d266f91..e89918289a4a53e9e2c405e7b0faa2f2b75f2c2b 100644 (file)
@@ -11,7 +11,7 @@
 # This file implements regression tests for SQLite library.  The
 # focus of this file is testing CHECK constraints
 #
-# $Id: check.test,v 1.11 2007/07/23 19:39:47 drh Exp $
+# $Id: check.test,v 1.12 2009/03/24 15:08:10 drh Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -115,11 +115,12 @@ do_test check-1.17 {
 } {4 11.0}
 
 do_test check-2.1 {
+breakpoint
   execsql {
     CREATE TABLE t2(
       x INTEGER CHECK( typeof(coalesce(x,0))=="integer" ),
-      y REAL CHECK( typeof(coalesce(y,0.1))=="real" ),
-      z TEXT CHECK( typeof(coalesce(z,''))=="text" )
+      y REAL CHECK( typeof(coalesce(y,0.1))=='real' ),
+      z TEXT CHECK( typeof(coalesce(z,''))=='text' )
     );
   }
 } {}
@@ -129,6 +130,8 @@ do_test check-2.2 {
     SELECT * FROM t2;
   }
 } {1 2.2 three}
+db close
+sqlite3 db test.db
 do_test check-2.3 {
   execsql {
     INSERT INTO t2 VALUES(NULL, NULL, NULL);
index 08c402c628aa723ef38609995ea33e22b2e053ba..9584b5df41fd1c2493b037af895bda5915bc5b76 100644 (file)
@@ -11,7 +11,7 @@
 #
 # Tests for the lookaside memory allocator.
 #
-# $Id: lookaside.test,v 1.8 2008/10/11 17:06:04 drh Exp $
+# $Id: lookaside.test,v 1.9 2009/03/24 15:08:10 drh Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -43,11 +43,11 @@ do_test lookaside-1.4 {
   db eval {CREATE TABLE t1(w,x,y,z);}
   foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0] break
   expr {$x==0 && $y<$z && $z==18}
-} {1}
+} {0}
 do_test lookaside-1.5 {
   foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 1] break
   expr {$x==0 && $y<$z && $z==18}
-} {1}
+} {0}
 do_test lookaside-1.6 {
   foreach {x y z} [sqlite3_db_status db SQLITE_DBSTATUS_LOOKASIDE_USED 0] break
   expr {$x==0 && $y==$z && $y<18}
index 68bc69eb4c4da75d449b88af0eb81ec52803e554..75580f3815fe978c08714658bd0e2a92ce8f4297 100644 (file)
@@ -9,7 +9,7 @@
 #
 #***********************************************************************
 #
-# $Id: mutex1.test,v 1.18 2009/03/20 13:15:31 drh Exp $
+# $Id: mutex1.test,v 1.19 2009/03/24 15:08:10 drh Exp $
 
 set testdir [file dirname $argv0]
 source $testdir/tester.tcl
@@ -114,7 +114,7 @@ ifcapable threadsafe {
     do_test mutex1.2.$mode.2 {
       sqlite3_initialize
       clear_mutex_counters
-      sqlite3 db test.db -nomutex 0
+      sqlite3 db test.db -nomutex 0 -fullmutex 0
       catchsql { CREATE TABLE abc(a, b, c) }
       db eval {
         INSERT INTO abc VALUES(1, 2, 3);