diff options
author | danielk1977 <danielk1977@noemail.net> | 2007-08-29 12:31:25 +0000 |
---|---|---|
committer | danielk1977 <danielk1977@noemail.net> | 2007-08-29 12:31:25 +0000 |
commit | a1644fd86384de5f37fc42a7fb871a8c9a12dab5 (patch) | |
tree | ccd22487d7d42e8de86c109311e9d8aef043154a | |
parent | 1fee73e74acab48412a3c596d4cd68deecadeef6 (diff) | |
download | sqlite-a1644fd86384de5f37fc42a7fb871a8c9a12dab5.tar.gz sqlite-a1644fd86384de5f37fc42a7fb871a8c9a12dab5.zip |
Modifications to the malloc failure tests to test transient and persistent failures. (CVS 4321)
FossilOrigin-Name: e38ef81b85feb5bff2ad8448f3438ff0ab36571e
40 files changed, 515 insertions, 304 deletions
@@ -1,5 +1,5 @@ -C Better\sasserts().\s\sBut\snow\ssome\sof\sthe\stests\sare\sbusted\sagain.\s(CVS\s4320) -D 2007-08-29T04:00:58 +C Modifications\sto\sthe\smalloc\sfailure\stests\sto\stest\stransient\sand\spersistent\sfailures.\s(CVS\s4321) +D 2007-08-29T12:31:26 F Makefile.in bfcc303429a5d9dcd552d807ee016c77427418c3 F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -81,29 +81,29 @@ F src/analyze.c a28b43a5f79bfa7616ab506bb917077242098469 F src/attach.c a52225c75b107be8c5bc144a2b6d20201be3f8f8 F src/auth.c 083c1205b45e3f52291ec539d396b4fc557856b3 F src/btmutex.c c04e67d99d949a12beb17209f307950f21313980 -F src/btree.c f9ea7d1e30d58cb1708aaa0c4c81a2761bd2bd13 +F src/btree.c 7079a423e0da7eec5147d102202e2b51038002cd F src/btree.h 7391b9e78ae441fe0fb3a6d3ae772770d35a4375 F src/btreeInt.h 1fa6510aa8601dc0358a5240d191335236d3cf76 -F src/build.c 99b0b0c44ce7673c00d8c0c97ce536e3b796328b -F src/callback.c a542236a68060caad378efa30006ca46cf77b1b2 +F src/build.c 915c4591ca7258b3144d53cd980bc3b4cc4a81e2 +F src/callback.c 77b302b0d41468dcda78c70e706e5b84577f0fa0 F src/complete.c 4cf68fd75d60257524cbe74f87351b9848399131 F src/date.c af235f38f50809abd0a96da3bb3e0cc32be6226e F src/delete.c 849846d06d29851dde0d9f424a5de5817eb140d1 F src/experimental.c 1b2d1a6cd62ecc39610e97670332ca073c50792b -F src/expr.c 978afdceb2693ef58ec0040a43fe57afc6e4236c -F src/func.c aa8a3a8db571c46e5197664ddbc2784006ee5525 -F src/hash.c 2f322979071dd2bdba7503b5276d66f028744382 +F src/expr.c 70660e0c5fc6375a03b3a7fc4eb6e66d96e5f6b1 +F src/func.c 4db632f1a81ed42a5a315136d3e90b7d8c76834c +F src/hash.c 8fe2fa52bd17869c87748e42cff5b1e7fbf90822 F src/hash.h 3ad3da76bfb954978d227bf495568b0e6da2c19e F src/insert.c 633322aef1799f6604fa805e12488bc628570b0c F src/journal.c 573911e274738b3fda813d4f0a6e319f8a9ecd36 -F src/legacy.c 7e1b1c57694e49cbadf561e2a7d9cd984dc743b5 +F src/legacy.c 4ac53191fad2e3c4d59bde1228879b2dc5a96d66 F src/limits.h 71ab25f17e35e0a9f3f6f234b8ed49cc56731d35 F src/loadext.c 8b31e2e0e961918fa045515459aee1c122d8c266 F src/main.c 58f71f9b0c583fbe74d13b6d3fdb509cac5871f3 -F src/malloc.c d4282f50964ab1ca31f504c97b7cf2fdb4d4195d +F src/malloc.c baed31e12b774080a2ee4cb88fbc6a41e601f64a F src/md5.c c5fdfa5c2593eaee2e32a5ce6c6927c986eaf217 F src/mem1.c afe2fbf6d7e8247c6c9f69c1481358b1cad60c08 -F src/mem2.c 1a2ca756a285b5365d667841508cc1f98938b8d8 +F src/mem2.c 2ba377e2f7d57ec36238456eb44371ac40aa0e94 F src/mutex.c 40e5ba09d56863895882a0204d93832e9960ea78 F src/mutex.h 4d3babe3a691533ac980967d394da512140b5143 F src/mutex_os2.c d47e9bd495583dd31263d8fe55160a31eb600a3c @@ -120,30 +120,30 @@ F src/os_unix.c 89bf24aa2475048a7833c45c522e7c6a81b83bb8 F src/os_unix.h 5768d56d28240d3fe4537fac08cc85e4fb52279e F src/os_win.c 3ffd3aacff4cb69848284e29dcec0feff23b0752 F src/os_win.h 41a946bea10f61c158ce8645e7646b29d44f122b -F src/pager.c 97fd0bb853e01e6890ad9aae97e7f9155330ebdc -F src/pager.h a9872db184613ae90ae80921f5fd956aa8f3522e +F src/pager.c f9830adfd3752c860a4024da5b871df5af4ed8a4 +F src/pager.h 1ac4468049348ec72df09d138fc1d7e3a9d0d3a6 F src/parse.y 2d2ce439dc6184621fb0b86f4fc5aca7f391a590 F src/pragma.c 9b989506a1b7c8aecd6befb8235e2f57a4aba7e5 -F src/prepare.c 67df42078f348876edf6124aca2c615de5743d75 -F src/printf.c 33d23a68e498006136ca9770579cf2d14a7ec68e +F src/prepare.c 1506fd279824b1f4bac97514966d0370101f9a6b +F src/printf.c e8cb99691b8370d0b721e2618db0ad01550e9b98 F src/random.c 4a22746501bf36b0a088c66e38dde5daba6a35da -F src/select.c 98c367bce3f38c5adfcc97de9ab5c79b0e5dc2b2 +F src/select.c c8045b76f4c2ae95227de5997d1649e601181c76 F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96 F src/shell.c ac29402b538515fa4697282387be9c1205e6e9eb -F src/sqlite.h.in 679cfc7927f843cd30a7b7a0fa17173752939a36 +F src/sqlite.h.in 1ef41c05bf64d580e77e6c57f8705cdf36f37be0 F src/sqlite3ext.h 9a26028378c288af500d8b94ed079666fed5806b -F src/sqliteInt.h 8225c78e94177fa4f1052c4cf0f779838bf56c19 +F src/sqliteInt.h 1dd9226a6f2f048222d5a5df38ab8cd26d8ece5d F src/sqliteLimit.h 1bcbbdfa856f8b71b561abb31edb864b0eca1d12 F src/table.c c725e47f6f3092b9a7b569fc58e408e2173ee008 F src/tclsqlite.c d76af53f45c9e9f7f7d39531fa4c7bee7d0adad6 F src/test1.c 8b20d1f05777ac480d3caf8d5f79ab7a543513fc -F src/test2.c 4f742e99ed1bea5c14692f627bdb59a146f30504 -F src/test3.c 637824fa560f9a4ffcec6b262febb0f57f400487 +F src/test2.c 4ab230fbdc0decfa7422f6a052b609ba54f4dfac +F src/test3.c de8f2a245d0adf4215487af8267c11ccca31e750 F src/test4.c c2c0f5dc907f1346f5d4b65eb5799f11eb9e4071 F src/test5.c 3a6a5717a149d7ca2e6d14f5be72cf7555d54dc4 F src/test6.c a6223d9d938aba83f20611a2c01680d8043cd2f7 F src/test7.c a9d509d0e9ad214b4772696f49f6e61be26213d1 -F src/test8.c a91b8d28341bbd168f5ba1ecad2be1008ddf15d1 +F src/test8.c 88e033aefdf5d5522dff46655a14ea7360fb1d26 F src/test9.c b46c8fe02ac7cca1a7316436d8d38d50c66f4b2f F src/test_async.c f222bd196b55a2e73b1427400d5aa97841787167 F src/test_autoext.c 855157d97aa28cf84233847548bfacda21807436 @@ -151,7 +151,7 @@ F src/test_btree.c c1308ba0b88ab577fa56c9e493a09829dfcded9c F src/test_config.c f0b911bb615d93a192647e76910dce65cbbcf3ad F src/test_hexio.c 82916f918687502658f02533b519c38cb180db6d F src/test_loadext.c 22065d601a18878e5542191001f0eaa5d77c0ed8 -F src/test_malloc.c 5f5566bb799e72cb328df8933ef9fdb9d90d270e +F src/test_malloc.c 9f26ba3469efc97f38100a899ca889d480778864 F src/test_md5.c 34599caee5b1c73dcf86ca31f55846fab8c19ef7 F src/test_schema.c 12c9de7661d6294eec2d57afbb52e2af1128084f F src/test_server.c 319f6b1a99bab5f7149387442243d6e65a8ab4eb @@ -161,17 +161,17 @@ F src/trigger.c 724a77d54609a33bde90618934fbeddfcc729a10 F src/update.c e89b980b443d44b68bfc0b1746cdb6308e049ac9 F src/utf.c 4af6259d5906b5a1bf3035cc387c4d7907bdd56e F src/util.c 3f9c0387b54f977726790f52ab92cd3d9379b367 -F src/vacuum.c 5ec133b69edf581a232af7e2b01f45c9f2b8be32 -F src/vdbe.c f578e7f0905489ced1f05bc418dd51c0f79f9a04 +F src/vacuum.c 38745037c63246d1b0669038257890cf89fc4578 +F src/vdbe.c 9f2ef520614425016881234965b8146ac771d7dc F src/vdbe.h 498e9ddade4baf70f2fc39e585670131dde07caa F src/vdbeInt.h 630145b9bfaa19190ab491f52658a7db550f2247 -F src/vdbeapi.c bdd0aea216744482dd1b7fab56de18ba5b6fbdf4 -F src/vdbeaux.c 347d2c1d68f326c90c27660da86849c51134fe1d +F src/vdbeapi.c 7737b6e07ea6547c6a83b2432832fe36309e2ce7 +F src/vdbeaux.c 7cc1e0b33e4d5b8259fd12b7bd28c2180903146c F src/vdbeblob.c d12ed95dac0992e1e372d079d76af047cc42f7c7 F src/vdbefifo.c 334c838c8f42d61a94813d136019ee566b5dc2f6 -F src/vdbemem.c 896fa3f8df9d2661eb15c7ce361857741b447268 -F src/vtab.c 72e5347cca4d55e55e180015f4dc78736f852e14 -F src/where.c 2776a0caf8cbbfd6ec79cfb1cd9bc25074055e5e +F src/vdbemem.c de0f4df15900f274e8fe43753f59ab115255b788 +F src/vtab.c 5b8d7ec77b9babba1b48072576bf30b43d543163 +F src/where.c beade77e34619553fa425d52891b1b102f35c933 F tclinstaller.tcl 4356d9d94d2b5ed5e68f9f0c80c4df3048dd7617 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 F test/all.test 0b2117b4ccbb3dc88a5259d1735a04d20584b92b @@ -212,9 +212,9 @@ F test/btree9.test 5d8711b241145b90f65dd1795d5dd8290846fa5e F test/busy.test 0271c854738e23ad76e10d4096a698e5af29d211 F test/cache.test 3ff445c445742a7b6b9ba6e1d62a25263f9424b9 F test/capi2.test 3c5066836cbaa9923bcc8282c010aa227bb5efd4 -F test/capi3.test e1b40aa127de9ecfcca707f359d68b993ef38b4c +F test/capi3.test 6297cd1f8f2767f3d125e53dd39ce5ee9a8143ea F test/capi3b.test 664eb55318132f292f2c436f90906f578cad6b97 -F test/capi3c.test 787ce050fd5e09e419c920f91c7f404e62e20cd8 +F test/capi3c.test 9b324b73317d65d864c56c355a06a5b5cfbcd5a2 F test/cast.test c4780b58b569bab4947458195410ae8791ac216b F test/check.test 024ed399600b799160378cf9d9f436bdf5dfd184 F test/collate1.test e3eaa48c21e150814be1a7b852d2a8af24458d04 @@ -312,7 +312,7 @@ F test/icu.test e6bfae7f625c88fd14df6f540fe835bdfc1e4329 F test/in.test 369cb2aa1eab02296b4ec470732fe8c131260b1d F test/in2.test b1f447f4f0f67e9f83ff931e7e2e30873f9ea055 F test/incrblob.test 7f95c929b719626443a996b105d2b0ff06f47818 -F test/incrblob_err.test 9f88036c34bfd98d8b4990b25c7315c73094c4a4 +F test/incrblob_err.test a25f71ad03eef61aa6b553dba5642c79b74e0e98 F test/incrvacuum.test 569347726ea5940c3359e3d8cabb3505595cb82f F test/incrvacuum2.test 82397ceb5941cbe852fd29bb33fcdf5665bc80c2 F test/incrvacuum_ioerr.test cb331403b8dea3c5bae6163861ff25037b0df56a @@ -345,19 +345,19 @@ F test/lock2.test 5f9557b775662c2a5ee435378f39e10d64f65cb3 F test/lock3.test 615111293cf32aa2ed16d01c6611737651c96fb9 F test/lock4.test 49e22396b9be6e047b3d35469f233be79153c9d5 F test/main.test e7212ce1023957c7209778cc87fa932bd79ba89a -F test/malloc.test dbfaedfca734283182db18a64416cf037c33648f -F test/malloc2.test 96dfa6318d3b7bcd41d4fe7e24d35196d465bf58 -F test/malloc3.test 65d323508c7c4e1fb5569d03a11070b0259ad2fe -F test/malloc4.test cf1e856fbc8ebde562838cb47adce0ebc2d4f7d2 +F test/malloc.test 52306f056e7359e68ed2e204f37f47a0e2020c57 +F test/malloc2.test c1a74f46a3581b56df29ff46a4e1c99b41c44ad9 +F test/malloc3.test 95211d9b2394252cacf836c62fd3d405c58f73bd +F test/malloc4.test 18dace2c0013aa616abb17b71ebb29095dfce12d F test/malloc5.test 9db9205a5d444e23043368f961f92641c7770199 F test/malloc6.test 3733b9bd4e039c3239f869c40edbb88172025e2e F test/malloc7.test dd66d8f82916becf1d29b6640e4f4855485570f8 F test/malloc8.test 5ff95278bc73e815e295971afcdd175f6ba19258 F test/malloc9.test fb99833476bc1a9b0b15573b9a56ae13af843ef9 -F test/mallocA.test f49b8df5e1d2a090830ef5a5f94174c3c930234b +F test/mallocA.test bec59bb8c9837cea463d1e76e62d0ea5113c4292 F test/mallocB.test 56fd615f3fea97180132a89dd1dd2068852ef6a0 -F test/mallocC.test b6b83dea1c3820a70d50b0378481b71bda44bcb3 -F test/malloc_common.tcl b649c5fc558a137ddf4c69867c89e2e335b342ec +F test/mallocC.test 3c6385336c82c9c2249c6c28ed23f430e1d66bd3 +F test/malloc_common.tcl 3c2fb94035e9085f8a687d724b6b61fdac8d961f F test/manydb.test 8de36b8d33aab5ef295b11d9e95310aeded31af8 F test/memdb.test a67bda4ff90a38f2b19f6c7f95aa7289e051d893 F test/memleak.test d2d2a1ff7105d32dc3fdf691458cf6cba58c7217 @@ -480,7 +480,7 @@ F test/vtab8.test e19fa4a538fcd1bb66c22825fa8f71618fb13583 F test/vtab9.test ea58d2b95d61955f87226381716b2d0b1d4e4f9b F test/vtabA.test 9cb6b1afead6fdd91bbdf1ca65c44ccfd9b10936 F test/vtab_alter.test 87617789086fd1767aa071e7805f1af7e1dac144 -F test/vtab_err.test f63f4c96bcd403076d49393136fb4fd9e0c5b402 +F test/vtab_err.test 71739e81853668857dd3bae05232c0dd6f5147cf F test/vtab_shared.test d631d1f820c38c18939d53aab1fc35db5f0a8094 F test/where.test 1bcde8984c63747ac6d6bafcacd20fd6e8a223de F test/where2.test 3249d426b3fc7a106713d784e1628307fc308d2e @@ -567,7 +567,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5 -P 844d40b8379d3374130e2d94f6e32c2cda34e0ca -R cf2e80aed60a12c772fc5c9b86deea93 -U drh -Z b6b3d6523c723ce59d75a6cacc6da8b8 +P e8060f85e7871572e2a38dc96f03e6e1f34cc8fd +R 6d16792962565a302345db7d8e56e541 +U danielk1977 +Z 9b2b7f4f6f1c4dee7c46f90167d9dbc1 diff --git a/manifest.uuid b/manifest.uuid index fb88b7b37..7b1e5c077 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e8060f85e7871572e2a38dc96f03e6e1f34cc8fd
\ No newline at end of file +e38ef81b85feb5bff2ad8448f3438ff0ab36571e
\ No newline at end of file diff --git a/src/btree.c b/src/btree.c index a955b384c..46553e524 100644 --- a/src/btree.c +++ b/src/btree.c @@ -9,7 +9,7 @@ ** May you share freely, never taking more than you give. ** ************************************************************************* -** $Id: btree.c,v 1.416 2007/08/29 04:00:58 drh Exp $ +** $Id: btree.c,v 1.417 2007/08/29 12:31:26 danielk1977 Exp $ ** ** This file implements a external (disk-based) database using BTrees. ** See the header comment on "btreeInt.h" for additional information. @@ -1212,7 +1212,8 @@ int sqlite3BtreeOpen( pBt->pageSize = get2byte(&zDbHeader[16]); if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ - pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, 0); + pBt->pageSize = 0; + sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); pBt->maxEmbedFrac = 64; /* 25% */ pBt->minEmbedFrac = 32; /* 12.5% */ pBt->minLeafFrac = 32; /* 12.5% */ @@ -1242,7 +1243,7 @@ int sqlite3BtreeOpen( } pBt->usableSize = pBt->pageSize - nReserve; assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ - sqlite3PagerSetPagesize(pBt->pPager, pBt->pageSize); + sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) /* Add the new BtShared object to the linked list sharable BtShareds. @@ -1488,6 +1489,7 @@ int sqlite3BtreeSyncDisabled(Btree *p){ ** bytes per page is left unchanged. */ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ + int rc = SQLITE_OK; BtShared *pBt = p->pBt; sqlite3BtreeEnter(p); if( pBt->pageSizeFixed ){ @@ -1501,11 +1503,12 @@ int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve){ ((pageSize-1)&pageSize)==0 ){ assert( (pageSize & 7)==0 ); assert( !pBt->pPage1 && !pBt->pCursor ); - pBt->pageSize = sqlite3PagerSetPagesize(pBt->pPager, pageSize); + pBt->pageSize = pageSize; + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize); } pBt->usableSize = pBt->pageSize - nReserve; sqlite3BtreeLeave(p); - return SQLITE_OK; + return rc; } /* diff --git a/src/build.c b/src/build.c index 8369df50b..047576134 100644 --- a/src/build.c +++ b/src/build.c @@ -22,7 +22,7 @@ ** COMMIT ** ROLLBACK ** -** $Id: build.c,v 1.440 2007/08/28 22:24:35 drh Exp $ +** $Id: build.c,v 1.441 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -241,6 +241,7 @@ void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ zSql = sqlite3VMPrintf(pParse->db, zFormat, ap); va_end(ap); if( zSql==0 ){ + pParse->db->mallocFailed = 1; return; /* A malloc must have failed */ } pParse->nested++; @@ -1562,9 +1563,13 @@ void sqlite3EndTable( } #ifndef SQLITE_OMIT_FOREIGN_KEY for(pFKey=p->pFKey; pFKey; pFKey=pFKey->pNextFrom){ + void *data; int nTo = strlen(pFKey->zTo) + 1; pFKey->pNextTo = sqlite3HashFind(&pSchema->aFKey, pFKey->zTo, nTo); - sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey); + data = sqlite3HashInsert(&pSchema->aFKey, pFKey->zTo, nTo, pFKey); + if( data==(void *)pFKey ){ + db->mallocFailed = 1; + } } #endif pParse->pNewTable = 0; @@ -2383,7 +2388,10 @@ void sqlite3CreateIndex( sqlite3_snprintf(sizeof(zBuf),zBuf,"_%d",n); zName = 0; sqlite3SetString(&zName, "sqlite_autoindex_", pTab->zName, zBuf, (char*)0); - if( zName==0 ) goto exit_create_index; + if( zName==0 ){ + db->mallocFailed = 1; + goto exit_create_index; + } } /* Check for authorization to create an index. diff --git a/src/callback.c b/src/callback.c index 181f26f1b..009cfd740 100644 --- a/src/callback.c +++ b/src/callback.c @@ -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.22 2007/08/22 20:18:22 drh Exp $ +** $Id: callback.c,v 1.23 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -361,11 +361,13 @@ void sqlite3SchemaFree(void *p){ Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ Schema * p; if( pBt ){ - p = (Schema *)sqlite3BtreeSchema(pBt,sizeof(Schema),sqlite3SchemaFree); + p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree); }else{ - p = (Schema *)sqlite3DbMallocZero(db,sizeof(Schema)); + p = (Schema *)sqlite3MallocZero(sizeof(Schema)); } - if( p && 0==p->file_format ){ + if( !p ){ + db->mallocFailed = 1; + }else if ( 0==p->file_format ){ sqlite3HashInit(&p->tblHash, SQLITE_HASH_STRING, 0); sqlite3HashInit(&p->idxHash, SQLITE_HASH_STRING, 0); sqlite3HashInit(&p->trigHash, SQLITE_HASH_STRING, 0); diff --git a/src/expr.c b/src/expr.c index 1d1fb0e18..778c65f3b 100644 --- a/src/expr.c +++ b/src/expr.c @@ -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.308 2007/08/22 20:18:22 drh Exp $ +** $Id: expr.c,v 1.309 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -221,13 +221,14 @@ static int codeCompare( ** is responsible for making sure the node eventually gets freed. */ Expr *sqlite3Expr( + sqlite3 *db, /* Handle for sqlite3DbMallocZero() (may be null) */ int op, /* Expression opcode */ Expr *pLeft, /* Left operand */ Expr *pRight, /* Right operand */ const Token *pToken /* Argument token */ ){ Expr *pNew; - pNew = sqlite3MallocZero( sizeof(Expr) ); + pNew = sqlite3DbMallocZero(db, sizeof(Expr)); if( pNew==0 ){ /* When malloc fails, delete pLeft and pRight. Expressions passed to ** this function must always be allocated with sqlite3Expr() for this @@ -273,11 +274,7 @@ Expr *sqlite3PExpr( Expr *pRight, /* Right operand */ const Token *pToken /* Argument token */ ){ - Expr *pNew = sqlite3Expr(op, pLeft, pRight, pToken); - if( pNew==0 ){ - pParse->db->mallocFailed = 1; - } - return pNew; + return sqlite3Expr(pParse->db, op, pLeft, pRight, pToken); } /* @@ -297,12 +294,11 @@ Expr *sqlite3RegisterExpr(Parse *pParse, Token *pToken){ int depth; if( pParse->nested==0 ){ sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", pToken); - return sqlite3Expr(TK_NULL, 0, 0, 0); + return sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); } if( v==0 ) return 0; - p = sqlite3Expr(TK_REGISTER, 0, 0, pToken); + p = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, pToken); if( p==0 ){ - pParse->db->mallocFailed = 1; return 0; /* Malloc failed */ } depth = atoi((char*)&pToken->z[1]); @@ -322,7 +318,7 @@ Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){ }else if( pRight==0 ){ return pLeft; }else{ - Expr *p = sqlite3Expr(TK_AND, pLeft, pRight, 0); + Expr *p = sqlite3Expr(db, TK_AND, pLeft, pRight, 0); if( p==0 ){ db->mallocFailed = 1; } @@ -1234,9 +1230,13 @@ static int lookupName( }else{ z = sqlite3StrDup(zCol); } - sqlite3ErrorMsg(pParse, zErr, z); - sqlite3_free(z); - pTopNC->nErr++; + if( z ){ + sqlite3ErrorMsg(pParse, zErr, z); + sqlite3_free(z); + pTopNC->nErr++; + }else{ + db->mallocFailed = 1; + } } /* If a column from a table in pSrcList is referenced, then record @@ -1668,7 +1668,7 @@ void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ VdbeComment((v, "# Init EXISTS result")); } sqlite3ExprDelete(pSel->pLimit); - pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one); + pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &one); if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){ return; } diff --git a/src/func.c b/src/func.c index 7b981df30..989e29527 100644 --- a/src/func.c +++ b/src/func.c @@ -16,7 +16,7 @@ ** sqliteRegisterBuildinFunctions() found at the bottom of the file. ** All other code has file scope. ** -** $Id: func.c,v 1.169 2007/08/23 02:47:53 drh Exp $ +** $Id: func.c,v 1.170 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -234,6 +234,14 @@ static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ sqlite3_result_double(context, r); } +static void *contextMalloc(sqlite3_context *context, int nByte){ + char *z = sqlite3_malloc(nByte); + if( !z && nByte>0 ){ + sqlite3_result_error_nomem(context); + } + return z; +} + /* ** Implementation of the upper() and lower() SQL functions. */ @@ -247,7 +255,7 @@ static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ /* Verify that the call to _bytes() does not invalidate the _text() pointer */ assert( z2==(char*)sqlite3_value_text(argv[0]) ); if( z2 ){ - z1 = sqlite3_malloc(n+1); + z1 = contextMalloc(context, n+1); if( z1 ){ memcpy(z1, z2, n+1); for(i=0; z1[i]; i++){ @@ -267,7 +275,7 @@ static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ /* Verify that the call to _bytes() does not invalidate the _text() pointer */ assert( z2==(char*)sqlite3_value_text(argv[0]) ); if( z2 ){ - z1 = sqlite3_malloc(n+1); + z1 = contextMalloc(context, n+1); if( z1 ){ memcpy(z1, z2, n+1); for(i=0; z1[i]; i++){ @@ -332,7 +340,7 @@ static void randomBlob( sqlite3_result_error_toobig(context); return; } - p = sqlite3_malloc(n); + p = contextMalloc(context, n); if( p ){ sqlite3Randomness(n, p); sqlite3_result_blob(context, (char*)p, n, sqlite3_free); @@ -665,10 +673,8 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ sqlite3_result_error_toobig(context); return; } - zText = (char *)sqlite3_malloc((2*nBlob)+4); - if( !zText ){ - sqlite3_result_error(context, "out of memory", -1); - }else{ + zText = (char *)contextMalloc(context, (2*nBlob)+4); + if( zText ){ int i; for(i=0; i<nBlob; i++){ zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F]; @@ -695,19 +701,19 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ sqlite3_result_error_toobig(context); return; } - z = sqlite3_malloc( i+n+3 ); - if( z==0 ) return; - z[0] = '\''; - for(i=0, j=1; zArg[i]; i++){ - z[j++] = zArg[i]; - if( zArg[i]=='\'' ){ - z[j++] = '\''; + z = contextMalloc(context, i+n+3); + if( z ){ + z[0] = '\''; + for(i=0, j=1; zArg[i]; i++){ + z[j++] = zArg[i]; + if( zArg[i]=='\'' ){ + z[j++] = '\''; + } } + z[j++] = '\''; + z[j] = 0; + sqlite3_result_text(context, z, j, sqlite3_free); } - z[j++] = '\''; - z[j] = 0; - sqlite3_result_text(context, z, j, SQLITE_TRANSIENT); - sqlite3_free(z); } } } @@ -732,15 +738,16 @@ static void hexFunc( return; } assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ - z = zHex = sqlite3_malloc(n*2 + 1); - if( zHex==0 ) return; - for(i=0; i<n; i++, pBlob++){ - unsigned char c = *pBlob; - *(z++) = hexdigits[(c>>4)&0xf]; - *(z++) = hexdigits[c&0xf]; + z = zHex = contextMalloc(context, n*2 + 1); + if( zHex ){ + for(i=0; i<n; i++, pBlob++){ + unsigned char c = *pBlob; + *(z++) = hexdigits[(c>>4)&0xf]; + *(z++) = hexdigits[c&0xf]; + } + *z = 0; + sqlite3_result_text(context, zHex, n*2, sqlite3_free); } - *z = 0; - sqlite3_result_text(context, zHex, n*2, sqlite3_free); } /* @@ -798,7 +805,7 @@ static void replaceFunc( assert( zRep==sqlite3_value_text(argv[2]) ); nOut = nStr + 1; assert( nOut<SQLITE_MAX_LENGTH ); - zOut = sqlite3_malloc((int)nOut); + zOut = contextMalloc(context, (int)nOut); if( zOut==0 ){ return; } @@ -817,6 +824,7 @@ static void replaceFunc( zOld = zOut; zOut = sqlite3_realloc(zOut, (int)nOut); if( zOut==0 ){ + sqlite3_result_error_nomem(context); sqlite3_free(zOld); return; } @@ -873,7 +881,7 @@ static void trimFunc( SQLITE_SKIP_UTF8(z); } if( nChar>0 ){ - azChar = sqlite3_malloc( nChar*(sizeof(char*)+1) ); + azChar = contextMalloc(context, nChar*(sizeof(char*)+1)); if( azChar==0 ){ return; } @@ -1110,8 +1118,9 @@ static void test_auxdata( sqlite3_value **argv ){ int i; - char *zRet = sqlite3MallocZero(nArg*2); + char *zRet = contextMalloc(pCtx, nArg*2); if( !zRet ) return; + memset(zRet, 0, nArg*2); for(i=0; i<nArg; i++){ char const *z = (char*)sqlite3_value_text(argv[i]); if( z ){ @@ -1125,8 +1134,11 @@ static void test_auxdata( } }else{ zRet[i*2] = '0'; - zAux = sqlite3StrDup(z); - sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); + zAux = contextMalloc(pCtx, strlen(z)+1); + if( zAux ){ + strcpy(zAux, z); + sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); + } } zRet[i*2+1] = ' '; } diff --git a/src/hash.c b/src/hash.c index fd1ceba98..e917197d6 100644 --- a/src/hash.c +++ b/src/hash.c @@ -12,7 +12,7 @@ ** This is the implementation of generic hash-tables ** used in SQLite. ** -** $Id: hash.c,v 1.21 2007/08/16 10:09:03 danielk1977 Exp $ +** $Id: hash.c,v 1.22 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" #include <assert.h> @@ -222,6 +222,14 @@ static void rehash(Hash *pH, int new_size){ int (*xHash)(const void*,int); /* The hash function */ assert( (new_size & (new_size-1))==0 ); + + /* There is a call to sqlite3_malloc() inside rehash(). If there is + ** already an allocation at pH->ht, then if this malloc() fails it + ** is benign (since failing to resize a hash table is a performance + ** hit only, not a fatal error). + */ + sqlite3MallocBenignFailure(pH->htsize>0); + new_ht = (struct _ht *)sqlite3MallocZero( new_size*sizeof(struct _ht) ); if( new_ht==0 ) return; if( pH->ht ) sqlite3_free(pH->ht); diff --git a/src/legacy.c b/src/legacy.c index 72637b268..c004b89e1 100644 --- a/src/legacy.c +++ b/src/legacy.c @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: legacy.c,v 1.21 2007/08/22 20:18:22 drh Exp $ +** $Id: legacy.c,v 1.22 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -46,6 +46,8 @@ int sqlite3_exec( int nCallback; if( zSql==0 ) return SQLITE_OK; + + sqlite3_mutex_enter(db->mutex); while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){ int nCol; char **azVals = 0; @@ -127,5 +129,6 @@ exec_out: } assert( (rc&db->errMask)==rc ); + sqlite3_mutex_leave(db->mutex); return rc; } diff --git a/src/malloc.c b/src/malloc.c index bd86636e9..5db775d8e 100644 --- a/src/malloc.c +++ b/src/malloc.c @@ -12,7 +12,7 @@ ** Memory allocation functions used throughout sqlite. ** ** -** $Id: malloc.c,v 1.11 2007/08/24 03:51:34 drh Exp $ +** $Id: malloc.c,v 1.12 2007/08/29 12:31:26 danielk1977 Exp $ */ #include "sqliteInt.h" #include <stdarg.h> @@ -82,11 +82,9 @@ void *sqlite3MallocZero(unsigned n){ ** the mallocFailed flag in the connection pointer. */ void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ - void *p = sqlite3_malloc(n); + void *p = sqlite3DbMallocRaw(db, n); if( p ){ memset(p, 0, n); - }else if( db ){ - db->mallocFailed = 1; } return p; } @@ -96,28 +94,40 @@ void *sqlite3DbMallocZero(sqlite3 *db, unsigned n){ ** the mallocFailed flag in the connection pointer. */ void *sqlite3DbMallocRaw(sqlite3 *db, unsigned n){ - void *p = sqlite3_malloc(n); - if( !p && db ){ - db->mallocFailed = 1; + void *p = 0; + if( !db || db->mallocFailed==0 ){ + p = sqlite3_malloc(n); + if( !p && db ){ + db->mallocFailed = 1; + } } return p; } +void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){ + void *pNew = 0; + if( db->mallocFailed==0 ){ + pNew = sqlite3_realloc(p, n); + if( !pNew ){ + db->mallocFailed = 1; + } + } + return pNew; +} + /* ** Attempt to reallocate p. If the reallocation fails, then free p ** and set the mallocFailed flag in the database connection. */ void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){ void *pNew; - pNew = sqlite3_realloc(p, n); + pNew = sqlite3DbRealloc(db, p, n); if( !pNew ){ sqlite3_free(p); - db->mallocFailed = 1; } return pNew; } - /* ** Make a copy of a string in memory obtained from sqliteMalloc(). These ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This @@ -211,6 +221,11 @@ void sqlite3SetString(char **pz, ...){ ** is set to SQLITE_NOMEM. */ int sqlite3ApiExit(sqlite3* db, int rc){ + /* If the db handle is not NULL, then we must hold the connection handle + ** mutex here. Otherwise the read (and possible write) of db->mallocFailed + ** is unsafe, as is the call to sqlite3Error(). + */ + assert( !db || sqlite3_mutex_held(db->mutex) ); if( db && db->mallocFailed ){ sqlite3Error(db, SQLITE_NOMEM, 0); db->mallocFailed = 0; @@ -218,3 +233,4 @@ int sqlite3ApiExit(sqlite3* db, int rc){ } return rc & (db ? db->errMask : 0xff); } + diff --git a/src/mem2.c b/src/mem2.c index 03f2c2039..d3cc9354a 100644 --- a/src/mem2.c +++ b/src/mem2.c @@ -12,7 +12,7 @@ ** This file contains the C functions that implement a memory ** allocation subsystem for use by SQLite. ** -** $Id: mem2.c,v 1.10 2007/08/24 04:15:00 drh Exp $ +** $Id: mem2.c,v 1.11 2007/08/29 12:31:26 danielk1977 Exp $ */ /* @@ -138,7 +138,9 @@ static struct { */ int iFail; /* Decrement and fail malloc when this is 1 */ int iReset; /* When malloc fails set iiFail to this value */ - int iFailCnt; /* Number of failures */ + int iFailCnt; /* Number of failures */ + int iBenignFailCnt; /* Number of benign failures */ + int iNextIsBenign; /* True if the next call to malloc may fail benignly */ /* ** sqlite3MallocDisallow() increments the following counter. @@ -247,6 +249,7 @@ static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){ */ static void sqlite3MemsysFailed(void){ mem.iFailCnt = 0; + mem.iBenignFailCnt = 0; } /* @@ -261,6 +264,7 @@ void *sqlite3_malloc(int nByte){ int totalSize; if( nByte<=0 ){ + mem.iNextIsBenign = 0; return 0; } if( mem.mutex==0 ){ @@ -282,6 +286,9 @@ void *sqlite3_malloc(int nByte){ sqlite3MemsysFailed(); /* A place to set a breakpoint */ } mem.iFailCnt++; + if( mem.iNextIsBenign ){ + mem.iBenignFailCnt++; + } }else{ p = malloc(totalSize); mem.iFail--; @@ -329,6 +336,7 @@ void *sqlite3_malloc(int nByte){ p = (void*)pInt; } sqlite3_mutex_leave(mem.mutex); + mem.iNextIsBenign = 0; return p; } @@ -475,16 +483,26 @@ void sqlite3_memdebug_dump(const char *zFilename){ ** This routine returns the number of simulated failures that have ** occurred since the previous call. */ -int sqlite3_memdebug_fail(int iFail, int iRepeat){ +int sqlite3_memdebug_fail(int iFail, int iRepeat, int *piBenign){ int n = mem.iFailCnt; + if( piBenign ){ + *piBenign = mem.iBenignFailCnt; + } mem.iFail = iFail+1; if( iRepeat>=0 ){ mem.iReset = iRepeat; } mem.iFailCnt = 0; + mem.iBenignFailCnt = 0; return n; } +void sqlite3MallocBenignFailure(int isBenign){ + if( isBenign ){ + mem.iNextIsBenign = 1; + } +} + /* ** The following two routines are used to assert that no memory ** allocations occur between one call and the next. The use of diff --git a/src/pager.c b/src/pager.c index 9bbb640ea..5901ca1ee 100644 --- a/src/pager.c +++ b/src/pager.c @@ -18,7 +18,7 @@ ** file simultaneously, or one process from reading the database while ** another is writing. ** -** @(#) $Id: pager.c,v 1.379 2007/08/28 22:24:35 drh Exp $ +** @(#) $Id: pager.c,v 1.380 2007/08/29 12:31:27 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_DISKIO #include "sqliteInt.h" @@ -699,6 +699,7 @@ static void pager_resize_hash_table(Pager *pPager, int N){ PgHdr **aHash, *pPg; assert( N>0 && (N&(N-1))==0 ); pagerLeave(pPager); + sqlite3MallocBenignFailure((int)pPager->aHash); aHash = sqlite3MallocZero( sizeof(aHash[0])*N ); pagerEnter(pPager); if( aHash==0 ){ @@ -2266,22 +2267,32 @@ void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){ } /* -** Set the page size. Return the new size. If the suggest new page -** size is inappropriate, then an alternative page size is selected -** and returned. +** Set the page size to *pPageSize. If the suggest new page size is +** inappropriate, then an alternative page size is set to that +** value before returning. */ -int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){ +int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){ + int rc = SQLITE_OK; + u16 pageSize = *pPageSize; assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) ); - if( pageSize && !pPager->memDb && pPager->nRef==0 ){ - pagerEnter(pPager); - pager_reset(pPager); - pPager->pageSize = pageSize; - setSectorSize(pPager); - pagerLeave(pPager); - sqlite3_free(pPager->pTmpSpace); - pPager->pTmpSpace = sqlite3_malloc(pageSize); + if( pageSize && pageSize!=pPager->pageSize + && !pPager->memDb && pPager->nRef==0 + ){ + char *pNew = (char *)sqlite3_malloc(pageSize); + if( !pNew ){ + rc = SQLITE_NOMEM; + }else{ + pagerEnter(pPager); + pager_reset(pPager); + pPager->pageSize = pageSize; + setSectorSize(pPager); + sqlite3_free(pPager->pTmpSpace); + pPager->pTmpSpace = pNew; + pagerLeave(pPager); + } } - return pPager->pageSize; + *pPageSize = pPager->pageSize; + return rc; } /* @@ -3267,7 +3278,7 @@ static int pagerSharedLock(Pager *pPager){ int fout = 0; int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL; assert( !pPager->tempFile ); - rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags,&fout); + rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, &fout); assert( rc!=SQLITE_OK || pPager->jfd->pMethods ); if( fout&SQLITE_OPEN_READONLY ){ rc = SQLITE_BUSY; @@ -3276,7 +3287,7 @@ static int pagerSharedLock(Pager *pPager){ } if( rc!=SQLITE_OK ){ pager_unlock(pPager); - return SQLITE_BUSY; + return (rc==SQLITE_NOMEM?rc:SQLITE_BUSY); } pPager->journalOpen = 1; pPager->journalStarted = 0; diff --git a/src/pager.h b/src/pager.h index 41d7f66c8..7402dd74e 100644 --- a/src/pager.h +++ b/src/pager.h @@ -13,7 +13,7 @@ ** subsystem. The page cache subsystem reads and writes a file a page ** at a time and provides a journal for rollback. ** -** @(#) $Id: pager.h,v 1.63 2007/08/28 22:24:35 drh Exp $ +** @(#) $Id: pager.h,v 1.64 2007/08/29 12:31:27 danielk1977 Exp $ */ #ifndef _PAGER_H_ @@ -58,7 +58,7 @@ int sqlite3PagerOpen(sqlite3_vfs *, Pager **ppPager, const char *, int, int); void sqlite3PagerSetBusyhandler(Pager*, BusyHandler *pBusyHandler); void sqlite3PagerSetDestructor(Pager*, void(*)(DbPage*,int)); void sqlite3PagerSetReiniter(Pager*, void(*)(DbPage*,int)); -int sqlite3PagerSetPagesize(Pager*, int); +int sqlite3PagerSetPagesize(Pager*, u16*); int sqlite3PagerMaxPageCount(Pager*, int); int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); void sqlite3PagerSetCachesize(Pager*, int); diff --git a/src/prepare.c b/src/prepare.c index cf6be6a3a..c7f2462f8 100644 --- a/src/prepare.c +++ b/src/prepare.c @@ -13,7 +13,7 @@ ** interface, and routines that contribute to loading the database schema ** from disk. ** -** $Id: prepare.c,v 1.59 2007/08/29 00:33:07 drh Exp $ +** $Id: prepare.c,v 1.60 2007/08/29 12:31:27 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -182,7 +182,8 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ rc = sqlite3InitCallback(&initData, 3, (char **)azArg, 0); if( rc ){ sqlite3SafetyOn(db); - return initData.rc; + rc = initData.rc; + goto error_out; } pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); if( pTab ){ @@ -204,7 +205,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ if( rc!=SQLITE_OK && rc!=SQLITE_EMPTY ){ sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); sqlite3BtreeLeave(pDb->pBt); - return rc; + goto error_out; } /* Get the database meta information. @@ -233,7 +234,7 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ sqlite3SetString(pzErrMsg, sqlite3ErrStr(rc), (char*)0); sqlite3BtreeCloseCursor(curMain); sqlite3BtreeLeave(pDb->pBt); - return rc; + goto error_out; } }else{ memset(meta, 0, sizeof(meta)); @@ -329,6 +330,11 @@ static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ rc = SQLITE_OK; } sqlite3BtreeLeave(pDb->pBt); + +error_out: + if( rc==SQLITE_NOMEM ){ + db->mallocFailed = 1; + } return rc; } @@ -422,6 +428,9 @@ static int schemaIsValid(sqlite3 *db){ } sqlite3BtreeCloseCursor(curTemp); } + if( rc==SQLITE_NOMEM ){ + db->mallocFailed = 1; + } } return allOk; } diff --git a/src/printf.c b/src/printf.c index 37390a5f0..d59c8fd7f 100644 --- a/src/printf.c +++ b/src/printf.c @@ -789,7 +789,9 @@ static char *base_vprintf( memcpy(sM.zText, sM.zBase, sM.nChar+1); } }else if( sM.nAlloc>sM.nChar+10 ){ - char *zNew = xRealloc(sM.zText, sM.nChar+1); + char *zNew; + sqlite3MallocBenignFailure(1); + zNew = xRealloc(sM.zText, sM.nChar+1); if( zNew ){ sM.zText = zNew; } diff --git a/src/select.c b/src/select.c index 7cf96bb1c..8383e2be3 100644 --- a/src/select.c +++ b/src/select.c @@ -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.356 2007/08/16 10:09:03 danielk1977 Exp $ +** $Id: select.c,v 1.357 2007/08/29 12:31:27 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -60,7 +60,7 @@ Select *sqlite3SelectNew( memset(pNew, 0, sizeof(*pNew)); } if( pEList==0 ){ - pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(TK_ALL,0,0,0), 0); + pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0,0,0), 0); } pNew->pEList = pEList; pNew->pSrc = pSrc; @@ -1364,12 +1364,12 @@ static int prepSelectStmt(Parse *pParse, Select *p){ continue; } } - pRight = sqlite3Expr(TK_ID, 0, 0, 0); + pRight = sqlite3PExpr(pParse, TK_ID, 0, 0, 0); if( pRight==0 ) break; setQuotedToken(pParse, &pRight->token, zName); if( zTabName && (longNames || pTabList->nSrc>1) ){ - Expr *pLeft = sqlite3Expr(TK_ID, 0, 0, 0); - pExpr = sqlite3Expr(TK_DOT, pLeft, pRight, 0); + Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, 0); + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); if( pExpr==0 ) break; setQuotedToken(pParse, &pLeft->token, zTabName); setToken(&pExpr->span, diff --git a/src/sqlite.h.in b/src/sqlite.h.in index 0bfdb7897..46d5d6048 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -30,7 +30,7 @@ ** the version number) and changes its name to "sqlite3.h" as ** part of the build process. ** -** @(#) $Id: sqlite.h.in,v 1.246 2007/08/28 15:47:45 drh Exp $ +** @(#) $Id: sqlite.h.in,v 1.247 2007/08/29 12:31:28 danielk1977 Exp $ */ #ifndef _SQLITE3_H_ #define _SQLITE3_H_ @@ -2414,6 +2414,7 @@ void sqlite3_result_double(sqlite3_context*, double); void sqlite3_result_error(sqlite3_context*, const char*, int); void sqlite3_result_error16(sqlite3_context*, const void*, int); void sqlite3_result_error_toobig(sqlite3_context*); +void sqlite3_result_error_nomem(sqlite3_context*); void sqlite3_result_int(sqlite3_context*, int); void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); void sqlite3_result_null(sqlite3_context*); diff --git a/src/sqliteInt.h b/src/sqliteInt.h index ef69cb639..29cf1022a 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.604 2007/08/28 16:34:43 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.605 2007/08/29 12:31:28 danielk1977 Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -1542,6 +1542,7 @@ char *sqlite3StrNDup(const char*, int); char *sqlite3DbStrDup(sqlite3*,const char*); char *sqlite3DbStrNDup(sqlite3*,const char*, int); void *sqlite3DbReallocOrFree(sqlite3 *, void *, int); +void *sqlite3DbRealloc(sqlite3 *, void *, int); char *sqlite3MPrintf(sqlite3*,const char*, ...); char *sqlite3VMPrintf(sqlite3*,const char*, va_list); @@ -1557,7 +1558,7 @@ void sqlite3DequoteExpr(sqlite3*, Expr*); int sqlite3KeywordCode(const unsigned char*, int); int sqlite3RunParser(Parse*, const char*, char **); void sqlite3FinishCoding(Parse*); -Expr *sqlite3Expr(int, Expr*, Expr*, const Token*); +Expr *sqlite3Expr(sqlite3*, int, Expr*, Expr*, const Token*); Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*); Expr *sqlite3RegisterExpr(Parse*,Token*); Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*); @@ -1832,9 +1833,11 @@ void sqlite3Parser(void*, int, Token, Parse*); #ifdef SQLITE_MEMDEBUG void sqlite3MallocDisallow(void); void sqlite3MallocAllow(void); + void sqlite3MallocBenignFailure(int); #else # define sqlite3MallocDisallow() # define sqlite3MallocAllow() +# define sqlite3MallocBenignFailure(x) #endif diff --git a/src/test2.c b/src/test2.c index 9480f8f7f..ded9547ce 100644 --- a/src/test2.c +++ b/src/test2.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test2.c,v 1.50 2007/08/21 10:44:16 drh Exp $ +** $Id: test2.c,v 1.51 2007/08/29 12:31:28 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -66,6 +66,7 @@ static int pager_open( int argc, /* Number of arguments */ const char **argv /* Text of each argument */ ){ + u16 pageSize; Pager *pPager; int nPage; int rc; @@ -82,7 +83,8 @@ static int pager_open( return TCL_ERROR; } sqlite3PagerSetCachesize(pPager, nPage); - sqlite3PagerSetPagesize(pPager, test_pagesize); + pageSize = test_pagesize; + sqlite3PagerSetPagesize(pPager, &pageSize); sqlite3_snprintf(sizeof(zBuf),zBuf,"%p",pPager); Tcl_AppendResult(interp, zBuf, 0); return TCL_OK; diff --git a/src/test3.c b/src/test3.c index fcfa37caf..d35efaab4 100644 --- a/src/test3.c +++ b/src/test3.c @@ -13,9 +13,10 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test3.c,v 1.82 2007/08/24 16:08:29 drh Exp $ +** $Id: test3.c,v 1.83 2007/08/29 12:31:28 danielk1977 Exp $ */ #include "sqliteInt.h" +#include "btreeInt.h" #include "tcl.h" #include <stdlib.h> #include <string.h> @@ -530,6 +531,7 @@ static int btree_pager_stats( return TCL_ERROR; } pBt = sqlite3TextToPtr(argv[1]); + sqlite3_mutex_enter(pBt->pSqlite->mutex); sqlite3BtreeEnter(pBt); a = sqlite3PagerStats(sqlite3BtreePager(pBt)); for(i=0; i<11; i++){ @@ -543,6 +545,7 @@ static int btree_pager_stats( Tcl_AppendElement(interp, zBuf); } sqlite3BtreeLeave(pBt); + sqlite3_mutex_leave(pBt->pSqlite->mutex); return TCL_OK; } @@ -1493,7 +1496,11 @@ static int btree_set_cache_size( } pBt = sqlite3TextToPtr(argv[1]); if( Tcl_GetInt(interp, argv[2], &nCache) ) return TCL_ERROR; + + sqlite3_mutex_enter(pBt->pSqlite->mutex); sqlite3BtreeSetCacheSize(pBt, nCache); + sqlite3_mutex_leave(pBt->pSqlite->mutex); + return TCL_OK; } diff --git a/src/test8.c b/src/test8.c index b27bd31c4..1b7c6957f 100644 --- a/src/test8.c +++ b/src/test8.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test8.c,v 1.54 2007/08/25 13:37:49 danielk1977 Exp $ +** $Id: test8.c,v 1.55 2007/08/29 12:31:28 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -361,6 +361,7 @@ static int echoConstructor( sqlite3_vtab **ppVtab, char **pzErr ){ + int rc; int i; echo_vtab *pVtab; @@ -404,9 +405,10 @@ static int echoConstructor( ** structure. If an error occurs, delete the sqlite3_vtab structure and ** return an error code. */ - if( echoDeclareVtab(pVtab, db) ){ + rc = echoDeclareVtab(pVtab, db); + if( rc!=SQLITE_OK ){ echoDestructor((sqlite3_vtab *)pVtab); - return SQLITE_ERROR; + return rc; } /* Success. Set *ppVtab and return */ @@ -646,14 +648,25 @@ static int echoFilter( ** If the third argument, doFree, is true, then sqlite3_free() is ** also called to free the buffer pointed to by zAppend. */ -static void string_concat(char **pzStr, char *zAppend, int doFree){ +static void string_concat(char **pzStr, char *zAppend, int doFree, int *pRc){ char *zIn = *pzStr; - if( zIn ){ - char *zTemp = zIn; - zIn = sqlite3_mprintf("%s%s", zIn, zAppend); - sqlite3_free(zTemp); + if( !zAppend && doFree && *pRc==SQLITE_OK ){ + *pRc = SQLITE_NOMEM; + } + if( *pRc!=SQLITE_OK ){ + sqlite3_free(zIn); + zIn = 0; }else{ - zIn = sqlite3_mprintf("%s", zAppend); + if( zIn ){ + char *zTemp = zIn; + zIn = sqlite3_mprintf("%s%s", zIn, zAppend); + sqlite3_free(zTemp); + }else{ + zIn = sqlite3_mprintf("%s", zAppend); + } + if( !zIn ){ + *pRc = SQLITE_NOMEM; + } } *pzStr = zIn; if( doFree ){ @@ -709,6 +722,9 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ useCost = 1; } else { zQuery = sqlite3_mprintf("SELECT count(*) FROM %Q", pVtab->zTableName); + if( !zQuery ){ + return SQLITE_NOMEM; + } rc = sqlite3_prepare(pVtab->db, zQuery, -1, &pStmt, 0); sqlite3_free(zQuery); if( rc!=SQLITE_OK ){ @@ -723,6 +739,9 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ } zQuery = sqlite3_mprintf("SELECT rowid, * FROM %Q", pVtab->zTableName); + if( !zQuery ){ + return SQLITE_NOMEM; + } for(ii=0; ii<pIdxInfo->nConstraint; ii++){ const struct sqlite3_index_constraint *pConstraint; struct sqlite3_index_constraint_usage *pUsage; @@ -759,7 +778,7 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ } else { zNew = sqlite3_mprintf(" %s %s %s ?", zSep, zCol, zOp); } - string_concat(&zQuery, zNew, 1); + string_concat(&zQuery, zNew, 1, &rc); zSep = "AND"; pUsage->argvIndex = ++nArg; @@ -779,7 +798,7 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ zCol = "rowid"; } zNew = sqlite3_mprintf(" ORDER BY %s %s", zCol, zDir); - string_concat(&zQuery, zNew, 1); + string_concat(&zQuery, zNew, 1, &rc); pIdxInfo->orderByConsumed = 1; } @@ -787,7 +806,7 @@ static int echoBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ appendToEchoModule(pVtab->interp, zQuery); if( !zQuery ){ - return SQLITE_NOMEM; + return rc; } pIdxInfo->idxNum = hashString(zQuery); pIdxInfo->idxStr = zQuery; @@ -843,26 +862,32 @@ int echoUpdate( if( nData>1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ char *zSep = " SET"; z = sqlite3_mprintf("UPDATE %Q", pVtab->zTableName); + if( !z ){ + rc = SQLITE_NOMEM; + } bindArgOne = (apData[1] && sqlite3_value_type(apData[1])==SQLITE_INTEGER); bindArgZero = 1; if( bindArgOne ){ - string_concat(&z, " SET rowid=?1 ", 0); + string_concat(&z, " SET rowid=?1 ", 0, &rc); zSep = ","; } for(i=2; i<nData; i++){ if( apData[i]==0 ) continue; string_concat(&z, sqlite3_mprintf( - "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1); + "%s %Q=?%d", zSep, pVtab->aCol[i-2], i), 1, &rc); zSep = ","; } - string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1); + string_concat(&z, sqlite3_mprintf(" WHERE rowid=?%d", nData), 1, &rc); } /* If apData[0] is an integer and nData==1 then do a DELETE */ else if( nData==1 && sqlite3_value_type(apData[0])==SQLITE_INTEGER ){ z = sqlite3_mprintf("DELETE FROM %Q WHERE rowid = ?1", pVtab->zTableName); + if( !z ){ + rc = SQLITE_NOMEM; + } bindArgZero = 1; } @@ -873,24 +898,27 @@ int echoUpdate( char *zValues = 0; zInsert = sqlite3_mprintf("INSERT INTO %Q (", pVtab->zTableName); + if( !zInsert ){ + rc = SQLITE_NOMEM; + } if( sqlite3_value_type(apData[1])==SQLITE_INTEGER ){ bindArgOne = 1; zValues = sqlite3_mprintf("?"); - string_concat(&zInsert, "rowid", 0); + string_concat(&zInsert, "rowid", 0, &rc); } assert((pVtab->nCol+2)==nData); for(ii=2; ii<nData; ii++){ string_concat(&zInsert, - sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1); + sqlite3_mprintf("%s%Q", zValues?", ":"", pVtab->aCol[ii-2]), 1, &rc); string_concat(&zValues, - sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1); + sqlite3_mprintf("%s?%d", zValues?", ":"", ii), 1, &rc); } - string_concat(&z, zInsert, 1); - string_concat(&z, ") VALUES(", 0); - string_concat(&z, zValues, 1); - string_concat(&z, ")", 0); + string_concat(&z, zInsert, 1, &rc); + string_concat(&z, ") VALUES(", 0, &rc); + string_concat(&z, zValues, 1, &rc); + string_concat(&z, ")", 0, &rc); } /* Anything else is an error */ @@ -899,7 +927,9 @@ int echoUpdate( return SQLITE_ERROR; } - rc = sqlite3_prepare(db, z, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3_prepare(db, z, -1, &pStmt, 0); + } assert( rc!=SQLITE_OK || pStmt ); sqlite3_free(z); if( rc==SQLITE_OK ) { @@ -935,43 +965,50 @@ static int echoTransactionCall(sqlite3_vtab *tab, const char *zCall){ appendToEchoModule(pVtab->interp, zCall); appendToEchoModule(pVtab->interp, z); sqlite3_free(z); - return SQLITE_OK; + return (z?SQLITE_OK:SQLITE_NOMEM); } static int echoBegin(sqlite3_vtab *tab){ + int rc; echo_vtab *pVtab = (echo_vtab *)tab; Tcl_Interp *interp = pVtab->interp; const char *zVal; - echoTransactionCall(tab, "xBegin"); + rc = echoTransactionCall(tab, "xBegin"); - /* Check if the $::echo_module_begin_fail variable is defined. If it is, - ** and it is set to the name of the real table underlying this virtual - ** echo module table, then cause this xSync operation to fail. - */ - zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY); - if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ - return SQLITE_ERROR; + if( rc==SQLITE_OK ){ + /* Check if the $::echo_module_begin_fail variable is defined. If it is, + ** and it is set to the name of the real table underlying this virtual + ** echo module table, then cause this xSync operation to fail. + */ + zVal = Tcl_GetVar(interp, "echo_module_begin_fail", TCL_GLOBAL_ONLY); + if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ + rc = SQLITE_ERROR; + } } - return SQLITE_OK; + return rc; } static int echoSync(sqlite3_vtab *tab){ + int rc; echo_vtab *pVtab = (echo_vtab *)tab; Tcl_Interp *interp = pVtab->interp; const char *zVal; - echoTransactionCall(tab, "xSync"); + rc = echoTransactionCall(tab, "xSync"); - /* Check if the $::echo_module_sync_fail variable is defined. If it is, - ** and it is set to the name of the real table underlying this virtual - ** echo module table, then cause this xSync operation to fail. - */ - zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY); - if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ - return -1; + if( rc==SQLITE_OK ){ + /* Check if the $::echo_module_sync_fail variable is defined. If it is, + ** and it is set to the name of the real table underlying this virtual + ** echo module table, then cause this xSync operation to fail. + */ + zVal = Tcl_GetVar(interp, "echo_module_sync_fail", TCL_GLOBAL_ONLY); + if( zVal && 0==strcmp(zVal, pVtab->zTableName) ){ + rc = -1; + } } - return SQLITE_OK; + return rc; } static int echoCommit(sqlite3_vtab *tab){ + sqlite3MallocBenignFailure(1); return echoTransactionCall(tab, "xCommit"); } static int echoRollback(sqlite3_vtab *tab){ diff --git a/src/test_malloc.c b/src/test_malloc.c index d09f3e899..a0b03c370 100644 --- a/src/test_malloc.c +++ b/src/test_malloc.c @@ -13,7 +13,7 @@ ** This file contains code used to implement test interfaces to the ** memory allocation subsystem. ** -** $Id: test_malloc.c,v 1.5 2007/08/24 03:51:34 drh Exp $ +** $Id: test_malloc.c,v 1.6 2007/08/29 12:31:28 danielk1977 Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -245,7 +245,12 @@ static int test_memdebug_dump( /* -** Usage: sqlite3_memdebug_fail COUNTER ?REPEAT? +** Usage: sqlite3_memdebug_fail COUNTER ?OPTIONS? +** +** where options are: +** +** -repeat <boolean> +** -benigncnt <varname> ** ** Arrange for a simulated malloc() failure after COUNTER successes. ** If REPEAT is 1 then all subsequent malloc()s fail. If REPEAT is @@ -263,23 +268,56 @@ static int test_memdebug_fail( int objc, Tcl_Obj *CONST objv[] ){ + int ii; int iFail; - int iRepeat; + int iRepeat = -1; + int iBenignCnt; + Tcl_Obj *pBenignCnt = 0; + int nFail = 0; - if( objc!=3 && objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "COUNTER ?REPEAT?"); + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "COUNTER ?OPTIONS?"); return TCL_ERROR; } if( Tcl_GetIntFromObj(interp, objv[1], &iFail) ) return TCL_ERROR; - if( objc==3 ){ - if( Tcl_GetIntFromObj(interp, objv[2], &iRepeat) ) return TCL_ERROR; - }else{ - iRepeat = -1; + + for(ii=2; ii<objc; ii+=2){ + int nOption; + char *zOption = Tcl_GetStringFromObj(objv[ii], &nOption); + char *zErr = 0; + + if( nOption>1 && strncmp(zOption, "-repeat", nOption)==0 ){ + if( ii==(objc-1) ){ + zErr = "option requires an argument: "; + }else{ + if( Tcl_GetIntFromObj(interp, objv[ii+1], &iRepeat) ){ + return TCL_ERROR; + } + } + }else if( nOption>1 && strncmp(zOption, "-benigncnt", nOption)==0 ){ + if( ii==(objc-1) ){ + zErr = "option requires an argument: "; + }else{ + pBenignCnt = objv[ii+1]; + } + }else{ + zErr = "unknown option: "; + } + + if( zErr ){ + Tcl_AppendResult(interp, zErr, zOption, 0); + return TCL_ERROR; + } } + #ifdef SQLITE_MEMDEBUG { - extern int sqlite3_memdebug_fail(int,int); - nFail = sqlite3_memdebug_fail(iFail, iRepeat); + extern int sqlite3_memdebug_fail(int,int,int*); + nFail = sqlite3_memdebug_fail(iFail, iRepeat, &iBenignCnt); + if( pBenignCnt ){ + Tcl_ObjSetVar2(interp, pBenignCnt, 0, Tcl_NewIntObj(iBenignCnt), 0); + } } #endif Tcl_SetObjResult(interp, Tcl_NewIntObj(nFail)); diff --git a/src/vacuum.c b/src/vacuum.c index ec396c236..73cccba26 100644 --- a/src/vacuum.c +++ b/src/vacuum.c @@ -14,7 +14,7 @@ ** Most of the code in this file may be omitted by defining the ** SQLITE_OMIT_VACUUM macro. ** -** $Id: vacuum.c,v 1.72 2007/08/24 11:43:37 drh Exp $ +** $Id: vacuum.c,v 1.73 2007/08/29 12:31:28 danielk1977 Exp $ */ #include "sqliteInt.h" #include "vdbeInt.h" @@ -25,6 +25,9 @@ */ static int execSql(sqlite3 *db, const char *zSql){ sqlite3_stmt *pStmt; + if( !zSql ){ + return SQLITE_NOMEM; + } if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ return sqlite3_errcode(db); } diff --git a/src/vdbe.c b/src/vdbe.c index 9f9e24044..d0baf886b 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -43,7 +43,7 @@ ** in this file for details. If in doubt, do not deviate from existing ** commenting and indentation practices when changing or adding code. ** -** $Id: vdbe.c,v 1.646 2007/08/28 23:28:08 drh Exp $ +** $Id: vdbe.c,v 1.647 2007/08/29 12:31:28 danielk1977 Exp $ */ #include "sqliteInt.h" #include <ctype.h> @@ -2637,7 +2637,8 @@ case OP_VerifyCookie: { /* no-push */ iMeta = 0; } if( rc==SQLITE_OK && iMeta!=pOp->p2 ){ - sqlite3SetString(&p->zErrMsg, "database schema has changed", (char*)0); + sqlite3_free(p->zErrMsg); + p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed"); /* If the schema-cookie from the database file matches the cookie ** stored with the in-memory representation of the schema, do ** not reload the schema from the database file. diff --git a/src/vdbeapi.c b/src/vdbeapi.c index 1ad103e82..6716256c0 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -234,6 +234,13 @@ void sqlite3_result_error_toobig(sqlite3_context *pCtx){ sqlite3VdbeMemSetZeroBlob(&pCtx->s, SQLITE_MAX_LENGTH+1); } +/* An SQLITE_NOMEM error. */ +void sqlite3_result_error_nomem(sqlite3_context *pCtx){ + assert( sqlite3_mutex_held(pCtx->s.db->mutex) ); + sqlite3VdbeMemSetNull(&pCtx->s); + pCtx->isError = 1; + pCtx->s.db->mallocFailed = 1; +} /* ** Execute the statement pStmt, either until a row of data is ready, the diff --git a/src/vdbeaux.c b/src/vdbeaux.c index 5b58588a6..55cb88ab3 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -115,15 +115,13 @@ static void resizeOpArray(Vdbe *p, int N){ VdbeOp *pNew; int nNew = N + 100*(!runMode); int oldSize = p->nOpAlloc; - pNew = sqlite3_realloc(p->aOp, nNew*sizeof(Op)); + pNew = sqlite3DbRealloc(p->db, p->aOp, nNew*sizeof(Op)); if( pNew ){ p->nOpAlloc = nNew; p->aOp = pNew; if( nNew>oldSize ){ memset(&p->aOp[oldSize], 0, (nNew-oldSize)*sizeof(Op)); } - }else{ - p->db->mallocFailed = 1; } } } diff --git a/src/vdbemem.c b/src/vdbemem.c index 3e3c89005..ef96b2fa2 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -260,9 +260,7 @@ int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ if( pMem->flags & MEM_Short ){ pMem->z = pMem->zShort; } - if( ctx.isError ){ - rc = SQLITE_ERROR; - } + rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK); } return rc; } diff --git a/src/vtab.c b/src/vtab.c index 70d7efc17..7559bdefa 100644 --- a/src/vtab.c +++ b/src/vtab.c @@ -11,7 +11,7 @@ ************************************************************************* ** This file contains code used to help implement virtual tables. ** -** $Id: vtab.c,v 1.54 2007/08/24 03:51:34 drh Exp $ +** $Id: vtab.c,v 1.55 2007/08/29 12:31:29 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_VIRTUALTABLE #include "sqliteInt.h" @@ -296,6 +296,7 @@ void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ int nName = strlen(zName) + 1; pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); if( pOld ){ + db->mallocFailed = 1; assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ return; } diff --git a/src/where.c b/src/where.c index 65c7dbfe2..6f93f67ed 100644 --- a/src/where.c +++ b/src/where.c @@ -16,7 +16,7 @@ ** so is applicable. Because this module is responsible for selecting ** indices, you might also think of this module as the "query optimizer". ** -** $Id: where.c,v 1.257 2007/08/16 11:36:15 danielk1977 Exp $ +** $Id: where.c,v 1.258 2007/08/29 12:31:29 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -791,7 +791,7 @@ static void exprAnalyze( for(i=0; i<2; i++){ Expr *pNewExpr; int idxNew; - pNewExpr = sqlite3Expr(ops[i], sqlite3ExprDup(db, pExpr->pLeft), + pNewExpr = sqlite3Expr(db, ops[i], sqlite3ExprDup(db, pExpr->pLeft), sqlite3ExprDup(db, pList->a[i].pExpr), 0); idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); exprAnalyze(pSrc, pWC, idxNew); @@ -858,7 +858,7 @@ static void exprAnalyze( } assert( pLeft!=0 ); pDup = sqlite3ExprDup(db, pLeft); - pNew = sqlite3Expr(TK_IN, pDup, 0, 0); + pNew = sqlite3Expr(db, TK_IN, pDup, 0, 0); if( pNew ){ int idxNew; transferJoinMarkings(pNew, pExpr); @@ -934,7 +934,7 @@ or_not_possible: prereqColumn = exprTableUsage(pMaskSet, pLeft); if( (prereqExpr & prereqColumn)==0 ){ Expr *pNewExpr; - pNewExpr = sqlite3Expr(TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0); + pNewExpr = sqlite3Expr(db, TK_MATCH, 0, sqlite3ExprDup(db, pRight), 0); idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); pNewTerm = &pWC->a[idxNew]; pNewTerm->prereqRight = prereqExpr; diff --git a/test/capi3.test b/test/capi3.test index 563d7aa88..066643bb5 100644 --- a/test/capi3.test +++ b/test/capi3.test @@ -11,7 +11,7 @@ # This file implements regression tests for SQLite library. The # focus of this script testing the callback-free C/C++ API. # -# $Id: capi3.test,v 1.53 2007/08/25 13:37:49 danielk1977 Exp $ +# $Id: capi3.test,v 1.54 2007/08/29 12:31:29 danielk1977 Exp $ # set testdir [file dirname $argv0] @@ -754,7 +754,7 @@ if {[info command sqlite3_memdebug_fail]!=""} { do_test capi3-10-1 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] - sqlite3_memdebug_fail 0 0 + sqlite3_memdebug_fail 0 catchsql { select * from sqlite_master; } @@ -768,7 +768,7 @@ if {[info command sqlite3_memdebug_fail]!=""} { } {out of memory} } db close - sqlite3_memdebug_fail -1 0 + sqlite3_memdebug_fail -1 } # The following tests - capi3-11.* - test that a COMMIT or ROLLBACK diff --git a/test/capi3c.test b/test/capi3c.test index 2d039a683..0ecad358c 100644 --- a/test/capi3c.test +++ b/test/capi3c.test @@ -13,7 +13,7 @@ # This is a copy of the capi3.test file that has been adapted to # test the new sqlite3_prepare_v2 interface. # -# $Id: capi3c.test,v 1.10 2007/08/25 13:37:49 danielk1977 Exp $ +# $Id: capi3c.test,v 1.11 2007/08/29 12:31:29 danielk1977 Exp $ # set testdir [file dirname $argv0] @@ -749,7 +749,7 @@ if {[info command sqlite3_memdebug_fail]!=""} { do_test capi3c-10-1 { sqlite3 db test.db set DB [sqlite3_connection_pointer db] - sqlite3_memdebug_fail 0 0 + sqlite3_memdebug_fail 0 catchsql { select * from sqlite_master; } @@ -763,7 +763,7 @@ if {[info command sqlite3_memdebug_fail]!=""} { } {out of memory} } db close - sqlite3_memdebug_fail -1 0 + sqlite3_memdebug_fail -1 } # The following tests - capi3c-11.* - test that a COMMIT or ROLLBACK diff --git a/test/incrblob_err.test b/test/incrblob_err.test index 9b61c4ec2..8f92f9555 100644 --- a/test/incrblob_err.test +++ b/test/incrblob_err.test @@ -9,7 +9,7 @@ # #*********************************************************************** # -# $Id: incrblob_err.test,v 1.5 2007/08/27 23:48:24 drh Exp $ +# $Id: incrblob_err.test,v 1.6 2007/08/29 12:31:29 danielk1977 Exp $ # set testdir [file dirname $argv0] @@ -79,7 +79,7 @@ do_malloc_test 3 -tclprep { error "out of memory" } } -sqlite3_memdebug_fail -1 0 +sqlite3_memdebug_fail -1 do_ioerr_test incrblob_err-4 -cksum 1 -sqlprep { CREATE TABLE blobs(k, v BLOB); diff --git a/test/malloc.test b/test/malloc.test index 6f9799386..de81cf720 100644 --- a/test/malloc.test +++ b/test/malloc.test @@ -16,7 +16,7 @@ # to see what happens in the library if a malloc were to really fail # due to an out-of-memory situation. # -# $Id: malloc.test,v 1.44 2007/08/22 20:18:22 drh Exp $ +# $Id: malloc.test,v 1.45 2007/08/29 12:31:29 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -215,7 +215,8 @@ if {$::sqlite_options(utf16)} { sqlite3_column_int $::STMT 0 sqlite3_column_text16 $::STMT 1 sqlite3_column_double $::STMT 1 - sqlite3_reset $::STMT + set rc [sqlite3_reset $::STMT] + if {$rc eq "SQLITE_NOMEM"} {error "out of memory"} sqlite3_bind_text16 $::STMT 1 $::bomstr 60 #catch {sqlite3_finalize $::STMT} #if {[lindex [sqlite_malloc_stat] 2]<=0} { diff --git a/test/malloc2.test b/test/malloc2.test index 0baee2a13..1047fa6cb 100644 --- a/test/malloc2.test +++ b/test/malloc2.test @@ -16,7 +16,7 @@ # Recovery from malloc() failures is automatic. But we keep these # tests around because you can never have too many test cases. # -# $Id: malloc2.test,v 1.6 2007/08/22 22:04:37 drh Exp $ +# $Id: malloc2.test,v 1.7 2007/08/29 12:31:29 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -65,7 +65,7 @@ proc do_malloc2_test {tn args} { # Run the SQL. Malloc number $::n is set to fail. A malloc() failure # may or may not be reported. - sqlite3_memdebug_fail $::n 1 + sqlite3_memdebug_fail $::n -repeat 1 do_test malloc2-$tn.$::n.2 { set res [catchsql [string trim $::mallocopts(-sql)]] set rc [expr { @@ -80,7 +80,7 @@ proc do_malloc2_test {tn args} { # If $::n is greater than the number of malloc() calls required to # execute the SQL, then this test is finished. Break out of the loop. - set nFail [sqlite3_memdebug_fail -1 -1] + set nFail [sqlite3_memdebug_fail -1] if {$nFail==0} break # Nothing should work now, because the allocator should refuse to diff --git a/test/malloc3.test b/test/malloc3.test index d96519d59..2d76a78f4 100644 --- a/test/malloc3.test +++ b/test/malloc3.test @@ -13,7 +13,7 @@ # correctly. The emphasis of these tests are the _prepare(), _step() and # _finalize() calls. # -# $Id: malloc3.test,v 1.11 2007/08/22 22:04:37 drh Exp $ +# $Id: malloc3.test,v 1.12 2007/08/29 12:31:29 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -560,7 +560,7 @@ proc run_test {arglist {pcstart 0} {iFailStart 1}} { set ::rollback_hook_count 0 set ac [sqlite3_get_autocommit $::DB] ;# Auto-Commit - sqlite3_memdebug_fail $iFail 1 + sqlite3_memdebug_fail $iFail -repeat 1 set rc [catch {db eval [lindex $v 1]} msg] ;# True error occurs set nac [sqlite3_get_autocommit $::DB] ;# New Auto-Commit @@ -575,7 +575,7 @@ proc run_test {arglist {pcstart 0} {iFailStart 1}} { } {1} } - set nFail [sqlite3_memdebug_fail -1 -1] + set nFail [sqlite3_memdebug_fail -1] if {$rc == 0} { # Successful execution of sql. Our "mallocs-until-failure" # count should be greater than 0. Otherwise a malloc() failed @@ -639,7 +639,7 @@ db cache size 0 run_test $::run_test_script 9 1 # run_test [lrange $::run_test_script 0 3] 0 63 -sqlite3_memdebug_fail -1 -1 +sqlite3_memdebug_fail -1 db close finish_test diff --git a/test/malloc4.test b/test/malloc4.test index 1d8ad0eb6..2adc6288b 100644 --- a/test/malloc4.test +++ b/test/malloc4.test @@ -12,7 +12,7 @@ # This file contains tests to ensure that the library handles malloc() failures # correctly. The emphasis in this file is on sqlite3_column_XXX() APIs. # -# $Id: malloc4.test,v 1.5 2007/08/23 02:47:53 drh Exp $ +# $Id: malloc4.test,v 1.6 2007/08/29 12:31:29 danielk1977 Exp $ #--------------------------------------------------------------------------- # NOTES ON EXPECTED BEHAVIOUR @@ -51,7 +51,7 @@ proc do_stmt_test {id sql} { } {1} # Set the Nth malloc() to fail. - sqlite3_memdebug_fail $n 1 + sqlite3_memdebug_fail $n -repeat 1 # Test malloc failure in the _name(), _name16(), decltype() and # decltype16() APIs. Calls that occur after the malloc() failure should diff --git a/test/mallocA.test b/test/mallocA.test index 84e96b4ca..1a30316e3 100644 --- a/test/mallocA.test +++ b/test/mallocA.test @@ -10,7 +10,7 @@ #*********************************************************************** # This file contains additional out-of-memory checks (see malloc.tcl). # -# $Id: mallocA.test,v 1.3 2007/08/22 22:04:37 drh Exp $ +# $Id: mallocA.test,v 1.4 2007/08/29 12:31:29 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -41,19 +41,19 @@ db close file copy test.db test.db.bu -do_malloc_test 1 -testdb test.db.bu -sqlbody { +do_malloc_test mallocA-1 -testdb test.db.bu -sqlbody { ANALYZE } -do_malloc_test 2 -testdb test.db.bu -sqlbody { +do_malloc_test mallocA-2 -testdb test.db.bu -sqlbody { REINDEX; } -do_malloc_test 3 -testdb test.db.bu -sqlbody { +do_malloc_test mallocA-3 -testdb test.db.bu -sqlbody { REINDEX t1; } -do_malloc_test 4 -testdb test.db.bu -sqlbody { +do_malloc_test mallocA-4 -testdb test.db.bu -sqlbody { REINDEX main.t1; } -do_malloc_test 5 -testdb test.db.bu -sqlbody { +do_malloc_test mallocA-5 -testdb test.db.bu -sqlbody { REINDEX nocase; } diff --git a/test/mallocC.test b/test/mallocC.test index f10c223a4..432bddf3c 100644 --- a/test/mallocC.test +++ b/test/mallocC.test @@ -12,7 +12,7 @@ # This file tests aspects of the malloc failure while parsing # CREATE TABLE statements in auto_vacuum mode. # -# $Id: mallocC.test,v 1.3 2007/08/22 22:04:37 drh Exp $ +# $Id: mallocC.test,v 1.4 2007/08/29 12:31:29 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -61,7 +61,7 @@ proc do_mallocC_test {tn args} { # Run the SQL. Malloc number $::n is set to fail. A malloc() failure # may or may not be reported. - sqlite3_memdebug_fail $::n 1 + sqlite3_memdebug_fail $::n -repeat 1 do_test mallocC-$tn.$::n.1 { set res [catchsql [string trim $::mallocopts(-sql)]] set rc [expr { @@ -76,7 +76,7 @@ proc do_mallocC_test {tn args} { # If $::n is greater than the number of malloc() calls required to # execute the SQL, then this test is finished. Break out of the loop. - set nFail [sqlite3_memdebug_fail -1 -1] + set nFail [sqlite3_memdebug_fail -1] if {$nFail==0} { break } diff --git a/test/malloc_common.tcl b/test/malloc_common.tcl index 3e31ef4ef..76194c724 100644 --- a/test/malloc_common.tcl +++ b/test/malloc_common.tcl @@ -42,68 +42,90 @@ proc do_malloc_test {tn args} { set start 1 } - set ::go 1 - for {set ::n $start} {$::go && $::n < 50000} {incr ::n} { - do_test $tn.$::n { + foreach ::iRepeat {0 1} { + set ::go 1 + for {set ::n $start} {$::go && $::n < 50000} {incr ::n} { - # Remove all traces of database files test.db and test2.db from the files - # system. Then open (empty database) "test.db" with the handle [db]. - # - catch {db close} - catch {file delete -force test.db} - catch {file delete -force test.db-journal} - catch {file delete -force test2.db} - catch {file delete -force test2.db-journal} - if {[info exists ::mallocopts(-testdb)]} { - file copy $::mallocopts(-testdb) test.db - } - catch {sqlite3 db test.db} - - # Execute any -tclprep and -sqlprep scripts. + # If $::iRepeat is 0, then the malloc() failure is transient - it + # fails and then subsequent calls succeed. If $::iRepeat is 1, + # then the failure is persistent - once malloc() fails it keeps + # failing. # - if {[info exists ::mallocopts(-tclprep)]} { - eval $::mallocopts(-tclprep) - } - if {[info exists ::mallocopts(-sqlprep)]} { - execsql $::mallocopts(-sqlprep) - } + set zRepeat "transient" + if {$::iRepeat} {set zRepeat "persistent"} - # Now set the ${::n}th malloc() to fail and execute the -tclbody and - # -sqlbody scripts. - # - sqlite3_memdebug_fail $::n 1 - set ::mallocbody {} - if {[info exists ::mallocopts(-tclbody)]} { - append ::mallocbody "$::mallocopts(-tclbody)\n" - } - if {[info exists ::mallocopts(-sqlbody)]} { - append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" - } - set v [catch $::mallocbody msg] - set failFlag [sqlite3_memdebug_fail -1 0] - set go [expr {$failFlag>0}] + do_test ${tn}.${zRepeat}.${::n} { + + # Remove all traces of database files test.db and test2.db + # from the file-system. Then open (empty database) "test.db" + # with the handle [db]. + # + catch {db close} + catch {file delete -force test.db} + catch {file delete -force test.db-journal} + catch {file delete -force test2.db} + catch {file delete -force test2.db-journal} + if {[info exists ::mallocopts(-testdb)]} { + file copy $::mallocopts(-testdb) test.db + } + catch {sqlite3 db test.db} + + # Execute any -tclprep and -sqlprep scripts. + # + if {[info exists ::mallocopts(-tclprep)]} { + eval $::mallocopts(-tclprep) + } + if {[info exists ::mallocopts(-sqlprep)]} { + execsql $::mallocopts(-sqlprep) + } + + # Now set the ${::n}th malloc() to fail and execute the -tclbody + # and -sqlbody scripts. + # + sqlite3_memdebug_fail $::n -repeat $::iRepeat + set ::mallocbody {} + if {[info exists ::mallocopts(-tclbody)]} { + append ::mallocbody "$::mallocopts(-tclbody)\n" + } + if {[info exists ::mallocopts(-sqlbody)]} { + append ::mallocbody "db eval {$::mallocopts(-sqlbody)}" + } + + # The following block sets local variables as follows: + # + # isFail - True if an error (any error) was reported by sqlite. + # nFail - The total number of simulated malloc() failures. + # nBenign - The number of benign simulated malloc() failures. + # + set isFail [catch $::mallocbody msg] + set nFail [sqlite3_memdebug_fail -1 -benigncnt nBenign] +#puts "isFail=$isFail nFail=$nFail nBenign=$nBenign msg=$msg" + # If one or more mallocs failed, run this loop body again. + # + set go [expr {$nFail>0}] - if {$failFlag==0} { - if {$v} { + if {($nFail-$nBenign)==0} { + if {$isFail} { + set v2 $msg + } else { + set isFail 1 + set v2 1 + } + } elseif {!$isFail} { set v2 $msg - } else { - set v 1 + } elseif {[info command db]=="" || [db errorcode]==7 + || $msg=="out of memory"} { set v2 1 + } else { + set v2 $msg } - } elseif {!$v} { - set v2 $msg - } elseif {[info command db]=="" || [db errorcode]==7 - || $msg=="out of memory"} { - set v2 1 - } else { - set v2 $msg + lappend isFail $v2 + } {1 1} + + if {[info exists ::mallocopts(-cleanup)]} { + catch [list uplevel #0 $::mallocopts(-cleanup)] msg } - lappend v $v2 - } {1 1} - - if {[info exists ::mallocopts(-cleanup)]} { - catch [list uplevel #0 $::mallocopts(-cleanup)] msg } } unset ::mallocopts diff --git a/test/vtab_err.test b/test/vtab_err.test index ccd01082b..117fae438 100644 --- a/test/vtab_err.test +++ b/test/vtab_err.test @@ -9,7 +9,7 @@ # #*********************************************************************** # -# $Id: vtab_err.test,v 1.6 2007/08/25 13:37:49 danielk1977 Exp $ +# $Id: vtab_err.test,v 1.7 2007/08/29 12:31:29 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -40,7 +40,7 @@ do_ioerr_test vtab_err-1 -tclprep { COMMIT; } -do_malloc_test vtab_err-2 -tclprep { +do_malloc_test vtab_err-2 -tclprep { register_echo_module [sqlite3_connection_pointer db] } -sqlbody { BEGIN; @@ -58,6 +58,6 @@ do_malloc_test vtab_err-2 -tclprep { COMMIT; } -sqlite3_memdebug_fail -1 0 +sqlite3_memdebug_fail -1 finish_test |