diff options
author | drh <drh@noemail.net> | 2008-03-18 00:07:10 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2008-03-18 00:07:10 +0000 |
commit | 5efaf070991267cf9c8b4b7e5b3ed1e512d50d41 (patch) | |
tree | ec05016771424a599c25f6036830b1f7746edd99 /src | |
parent | 0e8ebe51689d6fbb2595d521c639e73b387764c5 (diff) | |
download | sqlite-5efaf070991267cf9c8b4b7e5b3ed1e512d50d41.tar.gz sqlite-5efaf070991267cf9c8b4b7e5b3ed1e512d50d41.zip |
Add the ability to simulate out-of-memory errors when using the default
memory allocator, mem1.c. Fix a bug that this enhancement revealed. (CVS 4875)
FossilOrigin-Name: d55a5e1c11ef90534abd2e5f18d06dd4739ade70
Diffstat (limited to 'src')
-rw-r--r-- | src/mem1.c | 26 | ||||
-rw-r--r-- | src/test_malloc.c | 14 | ||||
-rw-r--r-- | src/vdbemem.c | 3 |
3 files changed, 24 insertions, 19 deletions
diff --git a/src/mem1.c b/src/mem1.c index 2b28a8cdb..87dd41afc 100644 --- a/src/mem1.c +++ b/src/mem1.c @@ -12,7 +12,7 @@ ** This file contains the C functions that implement a memory ** allocation subsystem for use by SQLite. ** -** $Id: mem1.c,v 1.16 2008/02/14 23:26:56 drh Exp $ +** $Id: mem1.c,v 1.17 2008/03/18 00:07:11 drh Exp $ */ #include "sqliteInt.h" @@ -137,10 +137,14 @@ void *sqlite3_malloc(int nBytes){ if( mem.alarmCallback!=0 && mem.nowUsed+nBytes>=mem.alarmThreshold ){ sqlite3MemsysAlarm(nBytes); } - p = malloc(nBytes+8); - if( p==0 ){ - sqlite3MemsysAlarm(nBytes); + if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ + p = 0; + }else{ p = malloc(nBytes+8); + if( p==0 ){ + sqlite3MemsysAlarm(nBytes); + p = malloc(nBytes+8); + } } if( p ){ p[0] = nBytes; @@ -205,12 +209,16 @@ void *sqlite3_realloc(void *pPrior, int nBytes){ if( mem.nowUsed+nBytes-nOld>=mem.alarmThreshold ){ sqlite3MemsysAlarm(nBytes-nOld); } - p = realloc(p, nBytes+8); - if( p==0 ){ - sqlite3MemsysAlarm(nBytes); - p = pPrior; - p--; + if( sqlite3FaultStep(SQLITE_FAULTINJECTOR_MALLOC) ){ + p = 0; + }else{ p = realloc(p, nBytes+8); + if( p==0 ){ + sqlite3MemsysAlarm(nBytes); + p = pPrior; + p--; + p = realloc(p, nBytes+8); + } } if( p ){ p[0] = nBytes; diff --git a/src/test_malloc.c b/src/test_malloc.c index 86a32059f..38708009a 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.15 2008/02/19 15:15:16 drh Exp $ +** $Id: test_malloc.c,v 1.16 2008/03/18 00:07:11 drh Exp $ */ #include "sqliteInt.h" #include "tcl.h" @@ -463,18 +463,14 @@ static int test_memdebug_pending( int objc, Tcl_Obj *CONST objv[] ){ + int nPending; if( objc!=1 ){ Tcl_WrongNumArgs(interp, 1, objv, ""); return TCL_ERROR; } - -#if defined(SQLITE_MEMDEBUG) || defined(SQLITE_POW2_MEMORY_SIZE) - { - int nPending = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_PENDING, - SQLITE_FAULTINJECTOR_MALLOC); - Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending)); - } -#endif + nPending = sqlite3_test_control(SQLITE_TESTCTRL_FAULT_PENDING, + SQLITE_FAULTINJECTOR_MALLOC); + Tcl_SetObjResult(interp, Tcl_NewIntObj(nPending)); return TCL_OK; } diff --git a/src/vdbemem.c b/src/vdbemem.c index 632151c19..1c72cb37a 100644 --- a/src/vdbemem.c +++ b/src/vdbemem.c @@ -81,7 +81,7 @@ int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ || (f & (MEM_Dyn|MEM_Static|MEM_Ephem))==MEM_Static ); - if( ((f&MEM_Dyn)==0 || pMem->xDel || sqlite3MallocSize(pMem->z)<n) ){ + if( (f&MEM_Dyn)==0 || pMem->xDel || sqlite3MallocSize(pMem->z)<n ){ /* Allocate the new buffer. The minimum allocation size is 32 bytes. */ char *z = 0; @@ -94,6 +94,7 @@ int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ z = sqlite3DbMallocRaw(pMem->db, (n>32?n:32)); } if( !z ){ + pMem->flags = MEM_Null; return SQLITE_NOMEM; } } |