From f4208043d69f052ff8fef45252f01e5ae27656f6 Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Tue, 6 Dec 2005 17:48:31 +0000 Subject: [PATCH] Allow VACUUM to detach the auxillary database after malloc() fails. (CVS 2804) FossilOrigin-Name: 6824a78bc7b8582fc5c3a6ab05dd3ed996fc99b3 --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/vacuum.c | 42 +++++++++++++++++++++++++----------------- test/malloc3.test | 4 ++-- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/manifest b/manifest index 49f3cec97d..b33dbc8d55 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Modify\sATTACH\sand\sDETACH\sto\sexecute\sat\sruntime\sinstead\sof\scompile\stime.\s(CVS\s2803) -D 2005-12-06T17:19:11 +C Allow\sVACUUM\sto\sdetach\sthe\sauxillary\sdatabase\safter\smalloc()\sfails.\s(CVS\s2804) +D 2005-12-06T17:48:32 F Makefile.in e3c6b3a38d734d41574c04f2fc90d18de2b87102 F Makefile.linux-gcc aee18d8a05546dcf1888bd4547e442008a49a092 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028 @@ -83,7 +83,7 @@ F src/trigger.c 388c13a2f07584decdb88413a63017391f9a7f7a F src/update.c ec8e540617b116725b5a55c8d6b4db8bc67fdd7d F src/utf.c bda5eb85039ef16f2d17004c1e18c96e1ab0a80c F src/util.c 88ebafb5708f04002e58a373386e3c8c31bfd858 -F src/vacuum.c b99062005217f72568c4023390e0331e1b4568b3 +F src/vacuum.c 4d64ce98a4cb130002cbf8aba018b2567773e6b1 F src/vdbe.c 7f4a2affee140b7b57952ab2c081a9c22b950638 F src/vdbe.h 8729a4ee16ff9aeab2af9667df3cf300ff978e13 F src/vdbeInt.h 0055c37eccbf3a189fd893a90f8eb6a5fa60c871 @@ -179,7 +179,7 @@ F test/lock3.test 615111293cf32aa2ed16d01c6611737651c96fb9 F test/main.test b12f01d49a5c805a33fa6c0ef168691f63056e79 F test/malloc.test a5ed721cf7d1b12602ede4f98c11b65ab1582cc0 F test/malloc2.test e6e321db96d6c94cb18bf82ad7215070c41e624e -F test/malloc3.test 4e669aa90dcd410cdef0ebd92d3b69b93578cc8f +F test/malloc3.test 1c7eebb2cf0f1fb1fbcaa1d6b3f25767c55aa6c9 F test/manydb.test d81debbf5871242e3b5df1d3bb5e14c50431b6f8 F test/memdb.test 1860e060be810bf0775bc57408a5b7c4954bcaea F test/memleak.test df2b2b96e77f8ba159a332299535b1e5f18e49ac @@ -325,7 +325,7 @@ F www/tclsqlite.tcl ddcf912ea48695603c8ed7efb29f0812ef8d1b49 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0 F www/version3.tcl a99cf5f6d8bd4d5537584a2b342f0fb9fa601d8b F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513 -P f5b58163d4520fa3e7137e8445a8ef19aae3e799 -R 5b292cae1efee3561e8e46d85db25e2a +P 5e04ec694add7a8331e3d6fbdfcaed51349ae7bc +R f7226b3a88877e57bdcf4f6eccd9d5fd U danielk1977 -Z adc67f11e55d6ddbfd0953632e9d61d6 +Z 48f4477e2bf6d1e22b59c59df71537c6 diff --git a/manifest.uuid b/manifest.uuid index 382185bb8d..71d5c058f5 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5e04ec694add7a8331e3d6fbdfcaed51349ae7bc \ No newline at end of file +6824a78bc7b8582fc5c3a6ab05dd3ed996fc99b3 \ No newline at end of file diff --git a/src/vacuum.c b/src/vacuum.c index 13784c820f..b3533acfe5 100644 --- a/src/vacuum.c +++ b/src/vacuum.c @@ -14,9 +14,10 @@ ** Most of the code in this file may be omitted by defining the ** SQLITE_OMIT_VACUUM macro. ** -** $Id: vacuum.c,v 1.50 2005/12/06 12:53:01 danielk1977 Exp $ +** $Id: vacuum.c,v 1.51 2005/12/06 17:48:32 danielk1977 Exp $ */ #include "sqliteInt.h" +#include "vdbeInt.h" #include "os.h" #ifndef SQLITE_OMIT_VACUUM @@ -102,6 +103,7 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ char *zSql = 0; int rc2; int saved_flags; /* Saved value of the db->flags */ + sqlite3_stmt *pDetach = 0; /* Save the current value of the write-schema flag before setting it. */ saved_flags = db->flags; @@ -148,6 +150,15 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ randomName((unsigned char*)&zTemp[nFilename+1]); } while( sqlite3Os.xFileExists(zTemp) ); + /* Before we even attach it, compile a DETACH statement for vacuum_db. This + ** way, if malloc() fails we can detach the database without needing to + ** dynamically allocate memory. + */ + rc = sqlite3_prepare(db, "DETACH vacuum_db", -1, &pDetach, 0); + if( rc!=SQLITE_OK ){ + goto end_of_vacuum; + } + /* Attach the temporary database as 'vacuum_db'. The synchronous pragma ** can be set to 'off' for this file, as it is not recovered if a crash ** occurs anyway. The integrity of the database is maintained by a @@ -287,14 +298,6 @@ int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ } end_of_vacuum: - /* If one of the execSql() calls above returned SQLITE_NOMEM, then the - ** mallocFailed flag will be clear (because execSql() calls sqlite3_exec()). - ** Fix this so the flag and return code match. - */ - if( rc==SQLITE_NOMEM ){ - sqlite3Tsd()->mallocFailed = 1; - } - /* Restore the original value of db->flags */ db->flags = saved_flags; @@ -307,18 +310,23 @@ end_of_vacuum: */ db->autoCommit = 1; - /* TODO: We need to detach vacuum_db (if it is attached) even if malloc() - ** failed. Right now trying to do so will cause an assert() to fail in ** - **_prepare() (because it should be impossible to call _prepare() with the ** - ** mallocFailed flag set). So really, we need to be able to detach a database - ** without calling malloc(). Which seems plausible. - */ - if( !sqlite3Tsd()->mallocFailed ){ - rc2 = execSql(db, "DETACH vacuum_db;"); + if( pDetach ){ + ((Vdbe *)pDetach)->expired = 0; + sqlite3_step(pDetach); + rc2 = sqlite3_finalize(pDetach); if( rc==SQLITE_OK ){ rc = rc2; } } + + /* If one of the execSql() calls above returned SQLITE_NOMEM, then the + ** mallocFailed flag will be clear (because execSql() calls sqlite3_exec()). + ** Fix this so the flag and return code match. + */ + if( rc==SQLITE_NOMEM ){ + sqlite3Tsd()->mallocFailed = 1; + } + if( zTemp ){ sqlite3Os.xDelete(zTemp); sqliteFree(zTemp); diff --git a/test/malloc3.test b/test/malloc3.test index 5ab5e758c6..940fe9b0fc 100644 --- a/test/malloc3.test +++ b/test/malloc3.test @@ -9,7 +9,7 @@ # #*********************************************************************** # -# $Id: malloc3.test,v 1.1 2005/12/06 12:57:59 danielk1977 Exp $ +# $Id: malloc3.test,v 1.2 2005/12/06 17:48:32 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -456,7 +456,7 @@ TEST 29 { # Test a simple multi-file transaction # file delete -force test2.db -PREP {ATTACH 'test2.db' AS aux;} +SQL {ATTACH 'test2.db' AS aux;} SQL {BEGIN} SQL {CREATE TABLE aux.tbl2(x, y, z)} SQL {INSERT INTO tbl2 VALUES(1, 2, 3)}