From 238746a65051d55737ad99ad15d365cde8986487 Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Thu, 19 Mar 2009 18:51:06 +0000 Subject: [PATCH] Fix a couple of fairly obscure cases where an assert() could fail following a malloc failure. (CVS 6360) FossilOrigin-Name: cc0d925669ddeb55048e88aa5b4f658be60b0962 --- manifest | 26 +++++++++++++------------- manifest.uuid | 2 +- src/legacy.c | 8 ++++---- src/main.c | 43 +++++++++++++++++++++++-------------------- src/prepare.c | 6 +++--- src/test_func.c | 25 ++++++++++++++++++++++++- src/vdbeapi.c | 5 ++++- src/vdbeblob.c | 6 +++--- src/vtab.c | 6 ++++-- test/malloc.test | 24 +++++++++++++++++++++++- 10 files changed, 102 insertions(+), 49 deletions(-) diff --git a/manifest b/manifest index a251af1de7..3a0a5af0ae 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\ssqlite3_step()\sto\sreturn\sSQLITE_LOCKED\sif\sa\sstatement\scannot\sbe\sre-compiled\sdue\sto\slocks\son\sthe\sshared-cache\sschema.\sAlso\sadd\sa\sblocking\swrapper\sof\ssqlite3_prepare_v2()\sto\sthe\stest\scode.\s(CVS\s6359) -D 2009-03-19T07:58:31 +C Fix\sa\scouple\sof\sfairly\sobscure\scases\swhere\san\sassert()\scould\sfail\sfollowing\sa\smalloc\sfailure.\s(CVS\s6360) +D 2009-03-19T18:51:07 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in 583e87706abc3026960ed759aff6371faf84c211 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -120,9 +120,9 @@ F src/hash.h 28f38ebb1006a5beedcb013bcdfe31befe7437ae F src/hwtime.h 4a1d45f4cae1f402ea19686acf24acf4f0cb53cb F src/insert.c 71286d081a919a27ef22eaeccbe2718f93dc6aa9 F src/journal.c e00df0c0da8413ab6e1bb7d7cab5665d4a9000d0 -F src/legacy.c 8b3b95d48d202614946d7ce7256e7ba898905c3b +F src/legacy.c 2ad5b52df322d0f132f66817095e0e79c8942611 F src/loadext.c 3f96631089fc4f3871a67f02f2e4fc7ea4d51edc -F src/main.c 76f953dabeed1096ae605a1a4c31aae0bbc426a1 +F src/main.c cb5ce39624f312888a9058d6fcacb54a9f6e5e85 F src/malloc.c b9c59b33539af74641362a7496ecc3efd6477f6d F src/mem0.c f2f84062d1f35814d6535c9f9e33de3bfb3b132c F src/mem1.c 3bfb39e4f60b0179713a7c087b2d4f0dc205735f @@ -150,7 +150,7 @@ F src/pcache.c fcf7738c83c4d3e9d45836b2334c8a368cc41274 F src/pcache.h 9b927ccc5a538e31b4c3bc7eec4f976db42a1324 F src/pcache1.c f12518540ba776df3051215c4244e9cdc06b09cd F src/pragma.c 22ed04836aab8ce134c53be1ca896f3ad20fabdb -F src/prepare.c 8c6ef35c30aa6b59a39b975efa4f90a66a5afb81 +F src/prepare.c 14e61702b09a325e50000e5f5f3156cd20c4afe1 F src/printf.c 9866a9a9c4a90f6d4147407f373df3fd5d5f9b6f F src/random.c 676b9d7ac820fe81e6fb2394ac8c10cff7f38628 F src/resolve.c 094e44450371fb27869eb8bf679aacbe51fdc56d @@ -179,7 +179,7 @@ F src/test_backup.c 5b41518c5499dafe65177b0813b71ac356ee9df1 F src/test_btree.c d7b8716544611c323860370ee364e897c861f1b0 F src/test_config.c a05378089b6773ba36b85727dedf9ec0a16424ce F src/test_devsym.c 9f4bc2551e267ce7aeda195f3897d0f30c5228f4 -F src/test_func.c a55c4d5479ff2eb5c0a22d4d88e9528ab59c953b +F src/test_func.c dd7bcaafb4e149702b506ede125ef6a4d4f6c471 F src/test_hexio.c 2f1122aa3f012fa0142ee3c36ce5c902a70cd12f F src/test_journal.c 632011e420a581614343e78bc2fe77bd409a1706 F src/test_loadext.c 97dc8800e46a46ed002c2968572656f37e9c0dd9 @@ -203,11 +203,11 @@ F src/vacuum.c 4929a585ef0fb1dfaf46302f8a9c4aa30c2d9cf5 F src/vdbe.c f8164c2830f82714a77b1f2a97c2e9c4efbcb3bb F src/vdbe.h d70a68bee196ab228914a3902c79dbd24342a0f2 F src/vdbeInt.h 53a2f4696871712646c77351904576cca6ad9752 -F src/vdbeapi.c e50f5f6dbd0c5354da25ee52fa5fa4bc0758ba40 +F src/vdbeapi.c 025d83f51f5e0a6e0fb55d603103d24825577967 F src/vdbeaux.c e9b76cf2ca8f78b692be984381cc4b27defc902a -F src/vdbeblob.c 2852bae14c87129835938db58a77c3121e3ae962 +F src/vdbeblob.c 3408f5e1ab62627300b9c9c3168cff1de65e1658 F src/vdbemem.c 543a79d722734d2f8b7ad70f08218c30bcc5bbf5 -F src/vtab.c bf409d2dc068e1bb82beeb9eef120ccfff541afb +F src/vtab.c 18d671f0a45d0159088070bc6b219a3105034c87 F src/walker.c 42bd3f00ca2ef5ae842304ec0d59903ef051412d F src/where.c ac555c8f6ef71a80944b31dcb212f7127c9ae30c F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2 @@ -438,7 +438,7 @@ F test/lock6.test 862aa71e97b288d6b3f92ba3313f51bd0b003776 F test/lookaside.test e69f822f13745f1d5c445c6e30e30f059f30c8e5 F test/main.test 187a9a1b5248ed74a83838c581c15ec6023b555b F test/make-where7.tcl 40bb740b37eead343eaf57b74ab72d2a5a304745 -F test/malloc.test 8180b651cc7e6b6be933c335db590ff1c33d8884 +F test/malloc.test 7388109bccc42888ad4e2cb01b2169eb98d4d283 F test/malloc3.test 4bc57f850b212f706f3e1b37c4eced1d5a727cd1 F test/malloc4.test 957337613002b7058a85116493a262f679f3a261 F test/malloc5.test 20d1a0884b03edf811bfd7005faade028367e7c8 @@ -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 097737e3689b9a7e32815fe9c6fc6eed796ae53c -R 32a96214111535313729ca99d5be6662 +P e8be1af922098e298902820730f8b28603bd6fae +R a5b48358dfad70bb132127a0cacc5d78 U danielk1977 -Z 02acbb631daf9f43302bd19b0b92f600 +Z ab897999fdfbec1ae24894bf472e0325 diff --git a/manifest.uuid b/manifest.uuid index ac2aac9fc4..402576af75 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e8be1af922098e298902820730f8b28603bd6fae \ No newline at end of file +cc0d925669ddeb55048e88aa5b4f658be60b0962 \ No newline at end of file diff --git a/src/legacy.c b/src/legacy.c index e83817da75..75de5dc911 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.31 2009/01/20 16:53:40 danielk1977 Exp $ +** $Id: legacy.c,v 1.32 2009/03/19 18:51:07 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -101,7 +101,7 @@ int sqlite3_exec( } if( xCallback(pArg, nCol, azVals, azCols) ){ rc = SQLITE_ABORT; - sqlite3_finalize(pStmt); + sqlite3VdbeFinalize((Vdbe *)pStmt); pStmt = 0; sqlite3Error(db, SQLITE_ABORT, 0); goto exec_out; @@ -109,7 +109,7 @@ int sqlite3_exec( } if( rc!=SQLITE_ROW ){ - rc = sqlite3_finalize(pStmt); + rc = sqlite3VdbeFinalize((Vdbe *)pStmt); pStmt = 0; if( rc!=SQLITE_SCHEMA ){ nRetry = 0; @@ -125,7 +125,7 @@ int sqlite3_exec( } exec_out: - if( pStmt ) sqlite3_finalize(pStmt); + if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt); sqlite3DbFree(db, azCols); rc = sqlite3ApiExit(db, rc); diff --git a/src/main.c b/src/main.c index 605f138ab5..5eddc02e6c 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ ** other files are for internal use by SQLite and should not be ** accessed by users of the library. ** -** $Id: main.c,v 1.532 2009/03/18 10:33:01 danielk1977 Exp $ +** $Id: main.c,v 1.533 2009/03/19 18:51:07 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -1245,15 +1245,15 @@ const char *sqlite3_errmsg(sqlite3 *db){ if( !sqlite3SafetyCheckSickOrOk(db) ){ return sqlite3ErrStr(SQLITE_MISUSE); } - if( db->mallocFailed ){ - return sqlite3ErrStr(SQLITE_NOMEM); - } sqlite3_mutex_enter(db->mutex); - assert( !db->mallocFailed ); - z = (char*)sqlite3_value_text(db->pErr); - assert( !db->mallocFailed ); - if( z==0 ){ - z = sqlite3ErrStr(db->errCode); + if( db->mallocFailed ){ + z = sqlite3ErrStr(SQLITE_NOMEM); + }else{ + z = (char*)sqlite3_value_text(db->pErr); + assert( !db->mallocFailed ); + if( z==0 ){ + z = sqlite3ErrStr(db->errCode); + } } sqlite3_mutex_leave(db->mutex); return z; @@ -1285,19 +1285,22 @@ const void *sqlite3_errmsg16(sqlite3 *db){ return (void *)misuse; } sqlite3_mutex_enter(db->mutex); - assert( !db->mallocFailed ); - z = sqlite3_value_text16(db->pErr); - if( z==0 ){ - sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), - SQLITE_UTF8, SQLITE_STATIC); + if( db->mallocFailed ){ + z = (void *)outOfMem; + }else{ z = sqlite3_value_text16(db->pErr); + if( z==0 ){ + sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), + SQLITE_UTF8, SQLITE_STATIC); + z = sqlite3_value_text16(db->pErr); + } + /* A malloc() may have failed within the call to sqlite3_value_text16() + ** above. If this is the case, then the db->mallocFailed flag needs to + ** be cleared before returning. Do this directly, instead of via + ** sqlite3ApiExit(), to avoid setting the database handle error message. + */ + db->mallocFailed = 0; } - /* A malloc() may have failed within the call to sqlite3_value_text16() - ** above. If this is the case, then the db->mallocFailed flag needs to - ** be cleared before returning. Do this directly, instead of via - ** sqlite3ApiExit(), to avoid setting the database handle error message. - */ - db->mallocFailed = 0; sqlite3_mutex_leave(db->mutex); return z; } diff --git a/src/prepare.c b/src/prepare.c index 0b9e747dd7..3367f7979b 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.110 2009/03/19 07:58:31 danielk1977 Exp $ +** $Id: prepare.c,v 1.111 2009/03/19 18:51:07 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -648,8 +648,8 @@ static int sqlite3Prepare( Vdbe *pVdbe = sParse.pVdbe; sqlite3VdbeSetSql(pVdbe, zSql, (int)(sParse.zTail-zSql), saveSqlFlag); } - if( rc!=SQLITE_OK || db->mallocFailed ){ - sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); + if( sParse.pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ + sqlite3VdbeFinalize(sParse.pVdbe); assert(!(*ppStmt)); }else{ *ppStmt = (sqlite3_stmt*)sParse.pVdbe; diff --git a/src/test_func.c b/src/test_func.c index 20f3307185..275359e387 100644 --- a/src/test_func.c +++ b/src/test_func.c @@ -12,7 +12,7 @@ ** Code for testing all sorts of SQLite interfaces. This code ** implements new SQL functions used by the test scripts. ** -** $Id: test_func.c,v 1.13 2008/08/28 02:26:07 drh Exp $ +** $Id: test_func.c,v 1.14 2009/03/19 18:51:07 danielk1977 Exp $ */ #include "sqlite3.h" #include "tcl.h" @@ -146,6 +146,25 @@ static void test_destructor_count( sqlite3_result_int(pCtx, test_destructor_count_var); } +/* +** The following aggregate function, test_agg_errmsg16(), takes zero +** arguments. It returns the text value returned by the sqlite3_errmsg16() +** API function. +*/ +void sqlite3BeginBenignMalloc(void); +void sqlite3EndBenignMalloc(void); +static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ +} +static void test_agg_errmsg16_final(sqlite3_context *ctx){ + const void *z; + sqlite3 * db = sqlite3_context_db_handle(ctx); + sqlite3_aggregate_context(ctx, 2048); + sqlite3BeginBenignMalloc(); + z = sqlite3_errmsg16(db); + sqlite3EndBenignMalloc(); + sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); +} + /* ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() ** interface. @@ -318,6 +337,10 @@ static int registerTestFunctions(sqlite3 *db){ sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); } + + sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, + test_agg_errmsg16_step, test_agg_errmsg16_final); + return SQLITE_OK; } diff --git a/src/vdbeapi.c b/src/vdbeapi.c index f0c0e0674c..9762e380c3 100644 --- a/src/vdbeapi.c +++ b/src/vdbeapi.c @@ -13,7 +13,7 @@ ** This file contains code use to implement APIs that are part of the ** VDBE. ** -** $Id: vdbeapi.c,v 1.154 2009/03/19 07:58:31 danielk1977 Exp $ +** $Id: vdbeapi.c,v 1.155 2009/03/19 18:51:07 danielk1977 Exp $ */ #include "sqliteInt.h" #include "vdbeInt.h" @@ -204,12 +204,14 @@ int sqlite3_finalize(sqlite3_stmt *pStmt){ rc = SQLITE_OK; }else{ Vdbe *v = (Vdbe*)pStmt; + sqlite3 *db = v->db; #if SQLITE_THREADSAFE sqlite3_mutex *mutex = v->db->mutex; #endif sqlite3_mutex_enter(mutex); stmtLruRemove(v); rc = sqlite3VdbeFinalize(v); + rc = sqlite3ApiExit(db, rc); sqlite3_mutex_leave(mutex); } return rc; @@ -234,6 +236,7 @@ int sqlite3_reset(sqlite3_stmt *pStmt){ stmtLruAdd(v); sqlite3VdbeMakeReady(v, -1, 0, 0, 0); assert( (rc & (v->db->errMask))==rc ); + rc = sqlite3ApiExit(v->db, rc); sqlite3_mutex_leave(v->db->mutex); } return rc; diff --git a/src/vdbeblob.c b/src/vdbeblob.c index a7caf2434f..82844675ba 100644 --- a/src/vdbeblob.c +++ b/src/vdbeblob.c @@ -12,7 +12,7 @@ ** ** This file contains code used to implement incremental BLOB I/O. ** -** $Id: vdbeblob.c,v 1.29 2009/03/05 03:48:07 shane Exp $ +** $Id: vdbeblob.c,v 1.30 2009/03/19 18:51:07 danielk1977 Exp $ */ #include "sqliteInt.h" @@ -248,8 +248,8 @@ int sqlite3_blob_open( blob_open_out: zErr[sizeof(zErr)-1] = '\0'; - if( rc!=SQLITE_OK || db->mallocFailed ){ - sqlite3_finalize((sqlite3_stmt *)v); + if( v && (rc!=SQLITE_OK || db->mallocFailed) ){ + sqlite3VdbeFinalize(v); } sqlite3Error(db, rc, (rc==SQLITE_OK?0:zErr)); rc = sqlite3ApiExit(db, rc); diff --git a/src/vtab.c b/src/vtab.c index 4bf6785038..a5865d8930 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.82 2009/03/16 13:19:36 danielk1977 Exp $ +** $Id: vtab.c,v 1.83 2009/03/19 18:51:07 danielk1977 Exp $ */ #ifndef SQLITE_OMIT_VIRTUALTABLE #include "sqliteInt.h" @@ -571,7 +571,9 @@ int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ } sParse.declareVtab = 0; - sqlite3_finalize((sqlite3_stmt*)sParse.pVdbe); + if( sParse.pVdbe ){ + sqlite3VdbeFinalize(sParse.pVdbe); + } sqlite3DeleteTable(sParse.pNewTable); sParse.pNewTable = 0; diff --git a/test/malloc.test b/test/malloc.test index 85c541bfa9..0ce93ba790 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.76 2009/02/25 08:56:47 danielk1977 Exp $ +# $Id: malloc.test,v 1.77 2009/03/19 18:51:07 danielk1977 Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -817,6 +817,28 @@ do_malloc_test 34 -tclprep { SELECT count(*) FROM abc; } +proc f {args} { error "Quite a long error!" } +do_malloc_test 35 -tclprep { + db func f f + set ::STMT [sqlite3_prepare db "SELECT f()" -1 DUMMY] + sqlite3_step $::STMT +} -tclbody { + sqlite3_finalize $::STMT +} -cleanup { + # At one point an assert( !db->mallocFailed ) could fail in the following + # call to sqlite3_errmsg(). Because sqlite3_finalize() had failed to clear + # the flag before returning. + sqlite3_errmsg16 db +} + +do_malloc_test 36 -sqlprep { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); +} -sqlbody { + SELECT test_agg_errmsg16(), group_concat(a) FROM t1 +} + # Ensure that no file descriptors were leaked. do_test malloc-99.X { catch {db close}