aboutsummaryrefslogtreecommitdiff
path: root/src/malloc.c
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2009-02-17 18:37:28 +0000
committerdrh <drh@noemail.net>2009-02-17 18:37:28 +0000
commit50b6568454237ede6e88fb2ef6df30bf2e7df842 (patch)
tree97694d206748175d168005e06c1fee23db22f96d /src/malloc.c
parentf37adcb40f35a9ee0172bcfe6f38943a24864818 (diff)
downloadsqlite-50b6568454237ede6e88fb2ef6df30bf2e7df842.tar.gz
sqlite-50b6568454237ede6e88fb2ef6df30bf2e7df842.zip
Add tests to double-check that nothing within SQLite ever tries to allocate
amounts of memory that are close to the maximum signed integer, leading to an integer overflow within malloc(). This is not currently a problem. The extra tests just insure it never becomes a problem. (CVS 6298) FossilOrigin-Name: f6ba7bb9152cffc9f67dfa7de12e36a3244b7e03
Diffstat (limited to 'src/malloc.c')
-rw-r--r--src/malloc.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/malloc.c b/src/malloc.c
index d9c036da6..468e057a8 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -12,7 +12,7 @@
**
** Memory allocation functions used throughout sqlite.
**
-** $Id: malloc.c,v 1.55 2009/02/17 16:29:11 danielk1977 Exp $
+** $Id: malloc.c,v 1.56 2009/02/17 18:37:29 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
@@ -266,7 +266,15 @@ static int mallocWithAlarm(int n, void **pp){
*/
void *sqlite3Malloc(int n){
void *p;
- if( n<=0 ){
+ if( n<=0 || NEVER(n>=0x7fffff00) ){
+ /* The NEVER(n>=0x7fffff00) term is added out of paranoia. We want to make
+ ** absolutely sure that there is nothing within SQLite that can cause a
+ ** memory allocation of a number of bytes which is near the maximum signed
+ ** integer value and thus cause an integer overflow inside of the xMalloc()
+ ** implementation. The n>=0x7fffff00 gives us 255 bytes of headroom. The
+ ** test should never be true because SQLITE_MAX_LENGTH should be much
+ ** less than 0x7fffff00 and it should catch large memory allocations
+ ** before they reach this point. */
p = 0;
}else if( sqlite3GlobalConfig.bMemstat ){
sqlite3_mutex_enter(mem0.mutex);
@@ -555,7 +563,8 @@ void *sqlite3Realloc(void *pOld, int nBytes){
if( pOld==0 ){
return sqlite3Malloc(nBytes);
}
- if( nBytes<=0 ){
+ if( nBytes<=0 || NEVER(nBytes>=0x7fffff00) ){
+ /* The NEVER(...) term is explained in comments on sqlite3Malloc() */
sqlite3_free(pOld);
return 0;
}