From 4693423b0904d5285815dc1e5a862c6e98fc15f1 Mon Sep 17 00:00:00 2001 From: drh Date: Sat, 20 Nov 2004 19:18:00 +0000 Subject: [PATCH] Rig the sqliteMalloc() routine so that we can make it fail multiple times in a row. Modify the malloc.test procedure to make malloc fail in this way and verify that the failures are still handled correctly. (CVS 2121) FossilOrigin-Name: 519bc9d9975bbdb4ba056799534d5c465e0cd042 --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/sqliteInt.h | 9 +++++---- src/test1.c | 22 +++++++++++++++++----- src/util.c | 10 +++++++--- test/malloc.test | 4 ++-- 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/manifest b/manifest index bd6e3351c7..c8ecb8db7a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\ssegfaults\sthat\smight\soccur\safter\sa\smalloc\sfailure.\s(CVS\s2119) -D 2004-11-20T18:13:10 +C Rig\sthe\ssqliteMalloc()\sroutine\sso\sthat\swe\scan\smake\sit\sfail\smultiple\stimes\nin\sa\srow.\s\sModify\sthe\smalloc.test\sprocedure\sto\smake\smalloc\sfail\sin\sthis\nway\sand\sverify\sthat\sthe\sfailures\sare\sstill\shandled\scorrectly.\s(CVS\s2121) +D 2004-11-20T19:18:01 F Makefile.in e747bb5ba34ccbdd81f79dcf1b2b33c02817c21d F Makefile.linux-gcc a9e5a0d309fa7c38e7c14d3ecf7690879d3a5457 F README a01693e454a00cc117967e3f9fdab2d4d52e9bc1 @@ -61,10 +61,10 @@ F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3 F src/select.c cf4b7952d6d214931c52636ee726f19ee2a275c5 F src/shell.c 55adda3cf3c1cc2f6c1919aac17b2318f9c2a96f F src/sqlite.h.in 6d0e82c24ef3f84a10b468119f3943a5dfc806c7 -F src/sqliteInt.h dd26056a172a5d488a78846b5ed8db6953db4e5d +F src/sqliteInt.h 8f3b4d0e90c294e0e22b20a9f7f32f2523b8894c F src/table.c 25b3ff2b39b7d87e8d4a5da0713d68dfc06cbee9 F src/tclsqlite.c 7f1a1a678140e6901c8954590ca2aabe50b48f71 -F src/test1.c 33818d5894681236240daaea1d379b463c7e9019 +F src/test1.c 21b1cc9358678da579d7aad8f16a40735a837078 F src/test2.c b11fa244fff02190707dd0879987c37c75e61fc8 F src/test3.c 6f1ec93e13632a004b527049535079eda84c459d F src/test4.c 7c6b9fc33dd1f3f93c7f1ee6e5e6d016afa6c1df @@ -73,7 +73,7 @@ F src/tokenize.c 2ad3d1ae1a0a70746db0b31a0a74f58050a3c39a F src/trigger.c 0c91b56182560263733e4b035acdb939bd1cf0e2 F src/update.c 395a2b270dfcbc96c20e40c9cb42b0533768ce30 F src/utf.c e45ce11be6922408cd381561721f6cca7d3b992a -F src/util.c 005fdf2d008f3429d081766ad6098fdd86d8d8e6 +F src/util.c 4a8db4e97a3cfda12ad8dda3e77dd2d00ad1de5e F src/vacuum.c ecb4a2c6f1ac5cc9b394dc64d3bb14ca650c4f60 F src/vdbe.c ba3a920731d43bcf2497d558238400369008531a F src/vdbe.h 067ca8d6750ba4f69a50284765e5883dee860181 @@ -145,7 +145,7 @@ F test/lock2.test 59c3dd7d9b24d1bf7ec91b2d1541c37e97939d5f F test/lock3.test 615111293cf32aa2ed16d01c6611737651c96fb9 F test/lock4.test 07768b4d4e942693d6036f1e6502199a3fa22a4f F test/main.test 5f9deae11b93336da1ccc5f91cf8be075c91ddf1 -F test/malloc.test 769b240d89a7ef3320d88919fdb6765f9395a51f +F test/malloc.test b4674cbb5a72b113eedaaf64fcd5c062e9957ded F test/memdb.test 34ee8598de307a16ccc3ac91b85cee9c668ae5ed F test/memleak.test f1fa233f8295dd1d955a00d5e5ee857850f27f29 F test/minmax.test c0f92d3f7b11656221735385f2c8b1878bbbdaf6 @@ -260,7 +260,7 @@ F www/tclsqlite.tcl 560ecd6a916b320e59f2917317398f3d59b7cc25 F www/vdbe.tcl 095f106d93875c94b47367384ebc870517431618 F www/version3.tcl 092a01f5ef430d2c4acc0ae558d74c4bb89638a0 F www/whentouse.tcl fdacb0ba2d39831e8a6240d05a490026ad4c4e4c -P aceaa5f6e97c93962f5d6d14de8a490efa619db4 -R 8f9d5443bb2a95ac89805536ee413fef +P 368774487e7a0266465f5a1c2701c9b14573d26c +R 2d696b4b112d3e16cd5eeec0f1da51b4 U drh -Z 96591b175a33effa711ce86d94861df3 +Z f7c4d9f04ce181b804404f872e7ac015 diff --git a/manifest.uuid b/manifest.uuid index 093b3a7fb3..83ca8f18d1 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -368774487e7a0266465f5a1c2701c9b14573d26c \ No newline at end of file +519bc9d9975bbdb4ba056799534d5c465e0cd042 \ No newline at end of file diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 810aa622a0..39ae7ae615 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.340 2004/11/16 15:50:20 danielk1977 Exp $ +** @(#) $Id: sqliteInt.h,v 1.341 2004/11/20 19:18:01 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -250,9 +250,10 @@ extern int sqlite3_malloc_failed; ** only. They only work if SQLITE_DEBUG is defined. */ #ifdef SQLITE_DEBUG -extern int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ -extern int sqlite3_nFree; /* Number of sqliteFree() calls */ -extern int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ +extern int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ +extern int sqlite3_nFree; /* Number of sqliteFree() calls */ +extern int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ +extern int sqlite3_iMallocReset; /* Set iMallocFail to this when it reaches 0 */ #endif /* diff --git a/src/test1.c b/src/test1.c index 8280bcad10..c7ba77cf53 100644 --- a/src/test1.c +++ b/src/test1.c @@ -13,7 +13,7 @@ ** is not included in the SQLite library. It is used for automated ** testing of the SQLite library. ** -** $Id: test1.c,v 1.115 2004/11/19 07:07:31 danielk1977 Exp $ +** $Id: test1.c,v 1.116 2004/11/20 19:18:01 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -746,10 +746,15 @@ static int sqlite3_mprintf_stronly( } /* -** Usage: sqlite_malloc_fail N +** Usage: sqlite_malloc_fail N ?REPEAT-INTERVAL? ** -** Rig sqliteMalloc() to fail on the N-th call. Turn off this mechanism -** and reset the sqlite3_malloc_failed variable is N==0. +** Rig sqliteMalloc() to fail on the N-th call and every REPEAT-INTERVAL call +** after that. If REPEAT-INTERVAL is 0 or is omitted, then only a single +** malloc will fail. If REPEAT-INTERVAL is 1 then all mallocs after the +** first failure will continue to fail on every call. If REPEAT-INTERVAL is +** 2 then every other malloc will fail. And so forth. +** +** Turn off this mechanism and reset the sqlite3_malloc_failed variable is N==0. */ #ifdef SQLITE_DEBUG static int sqlite_malloc_fail( @@ -759,12 +764,19 @@ static int sqlite_malloc_fail( char **argv /* Text of each argument */ ){ int n; - if( argc!=2 ){ + int rep; + if( argc!=2 && argc!=3 ){ Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], " N\"", 0); return TCL_ERROR; } if( Tcl_GetInt(interp, argv[1], &n) ) return TCL_ERROR; + if( argc==3 ){ + if( Tcl_GetInt(interp, argv[2], &rep) ) return TCL_ERROR; + }else{ + rep = 0; + } sqlite3_iMallocFail = n; + sqlite3_iMallocReset = rep; sqlite3_malloc_failed = 0; return TCL_OK; } diff --git a/src/util.c b/src/util.c index 316c616455..8994dfd611 100644 --- a/src/util.c +++ b/src/util.c @@ -14,7 +14,7 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.121 2004/11/04 04:34:15 drh Exp $ +** $Id: util.c,v 1.122 2004/11/20 19:18:01 drh Exp $ */ #include "sqliteInt.h" #include @@ -51,11 +51,15 @@ int sqlite3_malloc_failed = 0; /* ** For keeping track of the number of mallocs and frees. This -** is used to check for memory leaks. +** is used to check for memory leaks. The iMallocFail and iMallocReset +** values are used to simulate malloc() failures during testing in +** order to verify that the library correctly handles an out-of-memory +** condition. */ int sqlite3_nMalloc; /* Number of sqliteMalloc() calls */ int sqlite3_nFree; /* Number of sqliteFree() calls */ int sqlite3_iMallocFail; /* Fail sqliteMalloc() after this many calls */ +int sqlite3_iMallocReset = -1; /* When iMallocFail reaches 0, set to this */ #if SQLITE_DEBUG>1 static int memcnt = 0; #endif @@ -81,7 +85,7 @@ void *sqlite3Malloc_(int n, int bZero, char *zFile, int line){ fprintf(stderr,"**** failed to allocate %d bytes at %s:%d\n", n, zFile,line); #endif - sqlite3_iMallocFail--; + sqlite3_iMallocFail = sqlite3_iMallocReset; return 0; } } diff --git a/test/malloc.test b/test/malloc.test index ee9ae0274a..81d949b66b 100644 --- a/test/malloc.test +++ b/test/malloc.test @@ -14,7 +14,7 @@ # special feature is used 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.10 2004/06/30 09:49:24 danielk1977 Exp $ +# $Id: malloc.test,v 1.11 2004/11/20 19:18:01 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -33,7 +33,7 @@ for {set go 1; set i 1} {$go} {incr i} { catch {db close} catch {file delete -force test.db} catch {file delete -force test.db-journal} - sqlite_malloc_fail $i + sqlite_malloc_fail $i [expr {$i%4}] set v [catch {sqlite3 db test.db} msg] if {$v} { set msg "" -- 2.47.3