/ Check-in [fbc53da8]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment: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)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:fbc53da8c645935c74e49af2ab2cf447dc72ba4e
User & Date: shess 2007-03-16 18:30:54
Context
2007-03-17
07:22
Combine the two very similar definitions of (crashsql) in the test scripts. (CVS 3694) check-in: eaf434d5 user: danielk1977 tags: trunk
2007-03-16
18:30
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) check-in: fbc53da8 user: shess tags: trunk
2007-03-15
15:35
Save an if(...) clause made redundant by (3691). (CVS 3692) check-in: 8153edf8 user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to ext/fts1/fts1_porter.c.

    66     66   ** Create a new tokenizer instance.
    67     67   */
    68     68   static int porterCreate(
    69     69     int argc, const char * const *argv,
    70     70     sqlite3_tokenizer **ppTokenizer
    71     71   ){
    72     72     porter_tokenizer *t;
    73         -  t = (porter_tokenizer *) calloc(sizeof(porter_tokenizer), 1);
           73  +  t = (porter_tokenizer *) calloc(sizeof(*t), 1);
           74  +  if( t==NULL ) return SQLITE_NOMEM;
           75  +
    74     76     *ppTokenizer = &t->base;
    75     77     return SQLITE_OK;
    76     78   }
    77     79   
    78     80   /*
    79     81   ** Destroy a tokenizer
    80     82   */
................................................................................
    92     94   static int porterOpen(
    93     95     sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
    94     96     const char *zInput, int nInput,        /* String to be tokenized */
    95     97     sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
    96     98   ){
    97     99     porter_tokenizer_cursor *c;
    98    100   
    99         -  c = (porter_tokenizer_cursor *) malloc(sizeof(porter_tokenizer_cursor));
          101  +  c = (porter_tokenizer_cursor *) malloc(sizeof(*c));
          102  +  if( c==NULL ) return SQLITE_NOMEM;
          103  +
   100    104     c->zInput = zInput;
   101    105     if( zInput==0 ){
   102    106       c->nInput = 0;
   103    107     }else if( nInput<0 ){
   104    108       c->nInput = (int)strlen(zInput);
   105    109     }else{
   106    110       c->nInput = nInput;
................................................................................
   601    605       }
   602    606   
   603    607       if( c->iOffset>iStartOffset ){
   604    608         int n = c->iOffset-iStartOffset;
   605    609         if( n>c->nAllocated ){
   606    610           c->nAllocated = n+20;
   607    611           c->zToken = realloc(c->zToken, c->nAllocated);
          612  +        if( c->zToken==NULL ) return SQLITE_NOMEM;
   608    613         }
   609    614         porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
   610    615         *pzToken = c->zToken;
   611    616         *piStartOffset = iStartOffset;
   612    617         *piEndOffset = c->iOffset;
   613    618         *piPosition = c->iToken++;
   614    619         return SQLITE_OK;

Changes to ext/fts1/fts1_tokenizer1.c.

    57     57   */
    58     58   static int simpleCreate(
    59     59     int argc, const char * const *argv,
    60     60     sqlite3_tokenizer **ppTokenizer
    61     61   ){
    62     62     simple_tokenizer *t;
    63     63   
    64         -  t = (simple_tokenizer *) calloc(sizeof(simple_tokenizer), 1);
           64  +  t = (simple_tokenizer *) calloc(sizeof(*t), 1);
           65  +  if( t==NULL ) return SQLITE_NOMEM;
           66  +
    65     67     /* TODO(shess) Delimiters need to remain the same from run to run,
    66     68     ** else we need to reindex.  One solution would be a meta-table to
    67     69     ** track such information in the database, then we'd only want this
    68     70     ** information on the initial create.
    69     71     */
    70     72     if( argc>1 ){
    71     73       int i, n = strlen(argv[1]);
................................................................................
   107    109   static int simpleOpen(
   108    110     sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
   109    111     const char *pInput, int nBytes,        /* String to be tokenized */
   110    112     sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
   111    113   ){
   112    114     simple_tokenizer_cursor *c;
   113    115   
   114         -  c = (simple_tokenizer_cursor *) malloc(sizeof(simple_tokenizer_cursor));
          116  +  c = (simple_tokenizer_cursor *) malloc(sizeof(*c));
          117  +  if( c==NULL ) return SQLITE_NOMEM;
          118  +
   115    119     c->pInput = pInput;
   116    120     if( pInput==0 ){
   117    121       c->nBytes = 0;
   118    122     }else if( nBytes<0 ){
   119    123       c->nBytes = (int)strlen(pInput);
   120    124     }else{
   121    125       c->nBytes = nBytes;
................................................................................
   171    175       }
   172    176   
   173    177       if( c->iOffset>iStartOffset ){
   174    178         int i, n = c->iOffset-iStartOffset;
   175    179         if( n>c->nTokenAllocated ){
   176    180           c->nTokenAllocated = n+20;
   177    181           c->pToken = realloc(c->pToken, c->nTokenAllocated);
          182  +        if( c->pToken==NULL ) return SQLITE_NOMEM;
   178    183         }
   179    184         for(i=0; i<n; i++){
   180    185           /* TODO(shess) This needs expansion to handle UTF-8
   181    186           ** case-insensitivity.
   182    187           */
   183    188           unsigned char ch = p[iStartOffset+i];
   184    189           c->pToken[i] = ch<0x80 ? tolower(ch) : ch;

Changes to ext/fts2/fts2_porter.c.

    66     66   ** Create a new tokenizer instance.
    67     67   */
    68     68   static int porterCreate(
    69     69     int argc, const char * const *argv,
    70     70     sqlite3_tokenizer **ppTokenizer
    71     71   ){
    72     72     porter_tokenizer *t;
    73         -  t = (porter_tokenizer *) calloc(sizeof(porter_tokenizer), 1);
           73  +  t = (porter_tokenizer *) calloc(sizeof(*t), 1);
           74  +  if( t==NULL ) return SQLITE_NOMEM;
           75  +
    74     76     *ppTokenizer = &t->base;
    75     77     return SQLITE_OK;
    76     78   }
    77     79   
    78     80   /*
    79     81   ** Destroy a tokenizer
    80     82   */
................................................................................
    92     94   static int porterOpen(
    93     95     sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
    94     96     const char *zInput, int nInput,        /* String to be tokenized */
    95     97     sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
    96     98   ){
    97     99     porter_tokenizer_cursor *c;
    98    100   
    99         -  c = (porter_tokenizer_cursor *) malloc(sizeof(porter_tokenizer_cursor));
          101  +  c = (porter_tokenizer_cursor *) malloc(sizeof(*c));
          102  +  if( c==NULL ) return SQLITE_NOMEM;
          103  +
   100    104     c->zInput = zInput;
   101    105     if( zInput==0 ){
   102    106       c->nInput = 0;
   103    107     }else if( nInput<0 ){
   104    108       c->nInput = (int)strlen(zInput);
   105    109     }else{
   106    110       c->nInput = nInput;
................................................................................
   601    605       }
   602    606   
   603    607       if( c->iOffset>iStartOffset ){
   604    608         int n = c->iOffset-iStartOffset;
   605    609         if( n>c->nAllocated ){
   606    610           c->nAllocated = n+20;
   607    611           c->zToken = realloc(c->zToken, c->nAllocated);
          612  +        if( c->zToken==NULL ) return SQLITE_NOMEM;
   608    613         }
   609    614         porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
   610    615         *pzToken = c->zToken;
   611    616         *piStartOffset = iStartOffset;
   612    617         *piEndOffset = c->iOffset;
   613    618         *piPosition = c->iToken++;
   614    619         return SQLITE_OK;

Changes to ext/fts2/fts2_tokenizer1.c.

    57     57   */
    58     58   static int simpleCreate(
    59     59     int argc, const char * const *argv,
    60     60     sqlite3_tokenizer **ppTokenizer
    61     61   ){
    62     62     simple_tokenizer *t;
    63     63   
    64         -  t = (simple_tokenizer *) calloc(sizeof(simple_tokenizer), 1);
           64  +  t = (simple_tokenizer *) calloc(sizeof(*t), 1);
           65  +  if( t==NULL ) return SQLITE_NOMEM;
           66  +
    65     67     /* TODO(shess) Delimiters need to remain the same from run to run,
    66     68     ** else we need to reindex.  One solution would be a meta-table to
    67     69     ** track such information in the database, then we'd only want this
    68     70     ** information on the initial create.
    69     71     */
    70     72     if( argc>1 ){
    71     73       int i, n = strlen(argv[1]);
................................................................................
   107    109   static int simpleOpen(
   108    110     sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
   109    111     const char *pInput, int nBytes,        /* String to be tokenized */
   110    112     sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
   111    113   ){
   112    114     simple_tokenizer_cursor *c;
   113    115   
   114         -  c = (simple_tokenizer_cursor *) malloc(sizeof(simple_tokenizer_cursor));
          116  +  c = (simple_tokenizer_cursor *) malloc(sizeof(*c));
          117  +  if( c==NULL ) return SQLITE_NOMEM;
          118  +
   115    119     c->pInput = pInput;
   116    120     if( pInput==0 ){
   117    121       c->nBytes = 0;
   118    122     }else if( nBytes<0 ){
   119    123       c->nBytes = (int)strlen(pInput);
   120    124     }else{
   121    125       c->nBytes = nBytes;
................................................................................
   171    175       }
   172    176   
   173    177       if( c->iOffset>iStartOffset ){
   174    178         int i, n = c->iOffset-iStartOffset;
   175    179         if( n>c->nTokenAllocated ){
   176    180           c->nTokenAllocated = n+20;
   177    181           c->pToken = realloc(c->pToken, c->nTokenAllocated);
          182  +        if( c->pToken==NULL ) return SQLITE_NOMEM;
   178    183         }
   179    184         for(i=0; i<n; i++){
   180    185           /* TODO(shess) This needs expansion to handle UTF-8
   181    186           ** case-insensitivity.
   182    187           */
   183    188           unsigned char ch = p[iStartOffset+i];
   184    189           c->pToken[i] = ch<0x80 ? tolower(ch) : ch;