/ Check-in [714254cb]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

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

Overview
Comment:Fix a memory leak: a failure to deallocate the P3 parameter on a call to sqlite3VdbeChangeP3. (CVS 2695)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 714254cbc12564d44548707043fdcdffb17e4fde
User & Date: drh 2005-09-16 00:27:01
Context
2005-09-16
02:38
Fix a whole host of newly discovered memory leaks the occur after a failure of realloc(). (CVS 2696) check-in: 4686d649 user: drh tags: trunk
00:27
Fix a memory leak: a failure to deallocate the P3 parameter on a call to sqlite3VdbeChangeP3. (CVS 2695) check-in: 714254cb user: drh tags: trunk
2005-09-15
21:24
The table_info pragma now gives the order of columns in the primary key. (CVS 2694) check-in: 9b60f48d user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbeaux.c.

    64     64   static void resizeOpArray(Vdbe *p, int N){
    65     65     if( p->magic==VDBE_MAGIC_RUN ){
    66     66       assert( N==p->nOp );
    67     67       p->nOpAlloc = N;
    68     68       p->aOp = sqliteRealloc(p->aOp, N*sizeof(Op));
    69     69     }else if( p->nOpAlloc<N ){
    70     70       int oldSize = p->nOpAlloc;
           71  +    VdbeOp *pNew;
    71     72       p->nOpAlloc = N+100;
    72         -    p->aOp = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
    73         -    if( p->aOp ){
           73  +    pNew = sqliteRealloc(p->aOp, p->nOpAlloc*sizeof(Op));
           74  +    if( pNew ){
           75  +      p->aOp = pNew;
    74     76         memset(&p->aOp[oldSize], 0, (p->nOpAlloc-oldSize)*sizeof(Op));
    75     77       }
    76     78     }
    77     79   }
    78     80   
    79     81   /*
    80     82   ** Add a new instruction to the list of instructions current in the
................................................................................
   343    345   void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
   344    346     assert( val>=0 );
   345    347     assert( p->magic==VDBE_MAGIC_INIT );
   346    348     if( p && addr>=0 && p->nOp>addr && p->aOp ){
   347    349       p->aOp[addr].p2 = val;
   348    350     }
   349    351   }
          352  +
          353  +
          354  +/*
          355  +** Delete a P3 value if necessary.
          356  +*/
          357  +static void freeP3(int p3type, void *p3){
          358  +  if( p3 ){
          359  +    if( p3type==P3_DYNAMIC || p3type==P3_KEYINFO ){
          360  +      sqliteFree(p3);
          361  +    }
          362  +    if( p3type==P3_VDBEFUNC ){
          363  +      VdbeFunc *pVdbeFunc = (VdbeFunc *)p3;
          364  +      sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
          365  +      sqliteFree(pVdbeFunc);
          366  +    }
          367  +    if( p3type==P3_MEM ){
          368  +      sqlite3ValueFree((sqlite3_value*)p3);
          369  +    }
          370  +  }
          371  +}
          372  +
   350    373   
   351    374   /*
   352    375   ** Change the value of the P3 operand for a specific instruction.
   353    376   ** This routine is useful when a large program is loaded from a
   354    377   ** static array using sqlite3VdbeAddOpList but we want to make a
   355    378   ** few minor changes to the program.
   356    379   **
................................................................................
   373    396   **
   374    397   ** If addr<0 then change P3 on the most recently inserted instruction.
   375    398   */
   376    399   void sqlite3VdbeChangeP3(Vdbe *p, int addr, const char *zP3, int n){
   377    400     Op *pOp;
   378    401     assert( p->magic==VDBE_MAGIC_INIT );
   379    402     if( p==0 || p->aOp==0 ){
   380         -    if( n==P3_DYNAMIC || n==P3_KEYINFO_HANDOFF ){
   381         -      sqliteFree((void*)zP3);
   382         -    }
   383         -    if( n==P3_MEM ){
   384         -      sqlite3ValueFree((sqlite3_value *)zP3);
   385         -    }
          403  +    freeP3(n, (void*)*(char**)&zP3);
   386    404       return;
   387    405     }
   388    406     if( addr<0 || addr>=p->nOp ){
   389    407       addr = p->nOp - 1;
   390    408       if( addr<0 ) return;
   391    409     }
   392    410     pOp = &p->aOp[addr];
   393         -  if( pOp->p3 && pOp->p3type==P3_DYNAMIC ){
   394         -    sqliteFree(pOp->p3);
   395         -    pOp->p3 = 0;
   396         -  }
          411  +  freeP3(pOp->p3type, pOp->p3);
          412  +  pOp->p3 = 0;
   397    413     if( zP3==0 ){
   398    414       pOp->p3 = 0;
   399    415       pOp->p3type = P3_NOTUSED;
   400    416     }else if( n==P3_KEYINFO ){
   401    417       KeyInfo *pKeyInfo;
   402    418       int nField, nByte;
   403    419   
................................................................................
  1327   1343     }
  1328   1344     if( p->pNext ){
  1329   1345       p->pNext->pPrev = p->pPrev;
  1330   1346     }
  1331   1347     if( p->aOp ){
  1332   1348       for(i=0; i<p->nOp; i++){
  1333   1349         Op *pOp = &p->aOp[i];
  1334         -      if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
  1335         -        sqliteFree(pOp->p3);
  1336         -      }
  1337         -      if( pOp->p3type==P3_VDBEFUNC ){
  1338         -        VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
  1339         -        sqlite3VdbeDeleteAuxData(pVdbeFunc, 0);
  1340         -        sqliteFree(pVdbeFunc);
  1341         -      }
  1342         -      if( pOp->p3type==P3_MEM ){
  1343         -        sqlite3ValueFree((sqlite3_value*)pOp->p3);
  1344         -      }
         1350  +      freeP3(pOp->p3type, pOp->p3);
  1345   1351       }
  1346   1352       sqliteFree(p->aOp);
  1347   1353     }
  1348   1354     releaseMemArray(p->aVar, p->nVar);
  1349   1355     sqliteFree(p->aLabel);
  1350   1356     sqliteFree(p->aStack);
  1351   1357     releaseMemArray(p->aColName, p->nResColumn*2);