From 0d9f55a17767c3408a8ebd6278c3395abd0fef59 Mon Sep 17 00:00:00 2001 From: shess Date: Fri, 16 Mar 2007 18:30:54 +0000 Subject: [PATCH] Out-of-memory cleanup in tokenizers. Handle NULL return from malloc/calloc/realloc appropriately, and use sizeof(var) instead of sizeof(type) to make certain that we don't get a mismatch between them as the code rots. (CVS 3693) FossilOrigin-Name: fbc53da8c645935c74e49af2ab2cf447dc72ba4e --- ext/fts1/fts1_porter.c | 9 +++++++-- ext/fts1/fts1_tokenizer1.c | 9 +++++++-- ext/fts2/fts2_porter.c | 9 +++++++-- ext/fts2/fts2_tokenizer1.c | 9 +++++++-- manifest | 20 ++++++++++---------- manifest.uuid | 2 +- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/ext/fts1/fts1_porter.c b/ext/fts1/fts1_porter.c index 13bef7df59..7012634dc4 100644 --- a/ext/fts1/fts1_porter.c +++ b/ext/fts1/fts1_porter.c @@ -70,7 +70,9 @@ static int porterCreate( sqlite3_tokenizer **ppTokenizer ){ porter_tokenizer *t; - t = (porter_tokenizer *) calloc(sizeof(porter_tokenizer), 1); + t = (porter_tokenizer *) calloc(sizeof(*t), 1); + if( t==NULL ) return SQLITE_NOMEM; + *ppTokenizer = &t->base; return SQLITE_OK; } @@ -96,7 +98,9 @@ static int porterOpen( ){ porter_tokenizer_cursor *c; - c = (porter_tokenizer_cursor *) malloc(sizeof(porter_tokenizer_cursor)); + c = (porter_tokenizer_cursor *) malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + c->zInput = zInput; if( zInput==0 ){ c->nInput = 0; @@ -605,6 +609,7 @@ static int porterNext( if( n>c->nAllocated ){ c->nAllocated = n+20; c->zToken = realloc(c->zToken, c->nAllocated); + if( c->zToken==NULL ) return SQLITE_NOMEM; } porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); *pzToken = c->zToken; diff --git a/ext/fts1/fts1_tokenizer1.c b/ext/fts1/fts1_tokenizer1.c index a680c5ec0c..983737c5e7 100644 --- a/ext/fts1/fts1_tokenizer1.c +++ b/ext/fts1/fts1_tokenizer1.c @@ -61,7 +61,9 @@ static int simpleCreate( ){ simple_tokenizer *t; - t = (simple_tokenizer *) calloc(sizeof(simple_tokenizer), 1); + t = (simple_tokenizer *) calloc(sizeof(*t), 1); + if( t==NULL ) return SQLITE_NOMEM; + /* TODO(shess) Delimiters need to remain the same from run to run, ** else we need to reindex. One solution would be a meta-table to ** track such information in the database, then we'd only want this @@ -111,7 +113,9 @@ static int simpleOpen( ){ simple_tokenizer_cursor *c; - c = (simple_tokenizer_cursor *) malloc(sizeof(simple_tokenizer_cursor)); + c = (simple_tokenizer_cursor *) malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + c->pInput = pInput; if( pInput==0 ){ c->nBytes = 0; @@ -175,6 +179,7 @@ static int simpleNext( if( n>c->nTokenAllocated ){ c->nTokenAllocated = n+20; c->pToken = realloc(c->pToken, c->nTokenAllocated); + if( c->pToken==NULL ) return SQLITE_NOMEM; } for(i=0; ibase; return SQLITE_OK; } @@ -96,7 +98,9 @@ static int porterOpen( ){ porter_tokenizer_cursor *c; - c = (porter_tokenizer_cursor *) malloc(sizeof(porter_tokenizer_cursor)); + c = (porter_tokenizer_cursor *) malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + c->zInput = zInput; if( zInput==0 ){ c->nInput = 0; @@ -605,6 +609,7 @@ static int porterNext( if( n>c->nAllocated ){ c->nAllocated = n+20; c->zToken = realloc(c->zToken, c->nAllocated); + if( c->zToken==NULL ) return SQLITE_NOMEM; } porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); *pzToken = c->zToken; diff --git a/ext/fts2/fts2_tokenizer1.c b/ext/fts2/fts2_tokenizer1.c index 07619076e4..7af7a2f653 100644 --- a/ext/fts2/fts2_tokenizer1.c +++ b/ext/fts2/fts2_tokenizer1.c @@ -61,7 +61,9 @@ static int simpleCreate( ){ simple_tokenizer *t; - t = (simple_tokenizer *) calloc(sizeof(simple_tokenizer), 1); + t = (simple_tokenizer *) calloc(sizeof(*t), 1); + if( t==NULL ) return SQLITE_NOMEM; + /* TODO(shess) Delimiters need to remain the same from run to run, ** else we need to reindex. One solution would be a meta-table to ** track such information in the database, then we'd only want this @@ -111,7 +113,9 @@ static int simpleOpen( ){ simple_tokenizer_cursor *c; - c = (simple_tokenizer_cursor *) malloc(sizeof(simple_tokenizer_cursor)); + c = (simple_tokenizer_cursor *) malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + c->pInput = pInput; if( pInput==0 ){ c->nBytes = 0; @@ -175,6 +179,7 @@ static int simpleNext( if( n>c->nTokenAllocated ){ c->nTokenAllocated = n+20; c->pToken = realloc(c->pToken, c->nTokenAllocated); + if( c->pToken==NULL ) return SQLITE_NOMEM; } for(i=0; i