/ Check-in [2869ed28]
Login

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

Overview
Comment:Avoid using uninitialized variables after failures in the merge sort code.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2869ed28299b1c9f355ecc24635830f7f1249126
User & Date: drh 2011-08-31 23:57:22
Context
2011-09-02
15:08
Remove unused local variable. check-in: 61bda876 user: mistachkin tags: trunk
2011-09-01
15:32
Experimental code-generator changes to utilize new opcodes for sorting. check-in: bab2e560 user: drh tags: merge-sort
2011-08-31
23:57
Avoid using uninitialized variables after failures in the merge sort code. check-in: 2869ed28 user: drh tags: trunk
21:01
Formerly, we enabled fdatasync() on linux only. But now we learn that fdatasync() is not supported on Android. So we disable fdatasync() on Linux too. It can be reenabled at compile-time for those who really need it. check-in: 70b5b309 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/vdbesort.c.

   138    138   */
   139    139   static int vdbeSorterIterNext(
   140    140     sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
   141    141     VdbeSorterIter *pIter           /* Iterator to advance */
   142    142   ){
   143    143     int rc;                         /* Return Code */
   144    144     int nRead;                      /* Number of bytes read */
   145         -  int nRec;                       /* Size of record in bytes */
   146         -  int iOff;                       /* Size of serialized size varint in bytes */
          145  +  int nRec = 0;                   /* Size of record in bytes */
          146  +  int iOff = 0;                   /* Size of serialized size varint in bytes */
   147    147   
   148    148     nRead = pIter->iEof - pIter->iReadOff;
   149    149     if( nRead>5 ) nRead = 5;
   150    150     if( nRead<=0 ){
   151    151       /* This is an EOF condition */
   152    152       vdbeSorterIterZero(db, pIter);
   153    153       return SQLITE_OK;
   154    154     }
   155    155   
   156    156     rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
   157         -  iOff = getVarint32(pIter->aAlloc, nRec);
   158         -
   159         -  if( rc==SQLITE_OK && (iOff+nRec)>nRead ){
   160         -    int nRead2;                   /* Number of extra bytes to read */
   161         -    if( (iOff+nRec)>pIter->nAlloc ){
   162         -      int nNew = pIter->nAlloc*2;
   163         -      while( (iOff+nRec)>nNew ) nNew = nNew*2;
   164         -      pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
   165         -      if( !pIter->aAlloc ) return SQLITE_NOMEM;
   166         -      pIter->nAlloc = nNew;
          157  +  if( rc==SQLITE_OK ){
          158  +    iOff = getVarint32(pIter->aAlloc, nRec);
          159  +    if( (iOff+nRec)>nRead ){
          160  +      int nRead2;                   /* Number of extra bytes to read */
          161  +      if( (iOff+nRec)>pIter->nAlloc ){
          162  +        int nNew = pIter->nAlloc*2;
          163  +        while( (iOff+nRec)>nNew ) nNew = nNew*2;
          164  +        pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
          165  +        if( !pIter->aAlloc ) return SQLITE_NOMEM;
          166  +        pIter->nAlloc = nNew;
          167  +      }
          168  +  
          169  +      nRead2 = iOff + nRec - nRead;
          170  +      rc = sqlite3OsRead(
          171  +          pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
          172  +      );
   167    173       }
   168         -
   169         -    nRead2 = iOff + nRec - nRead;
   170         -    rc = sqlite3OsRead(
   171         -        pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
   172         -    );
   173    174     }
   174    175   
   175         -  assert( nRec>0 || rc!=SQLITE_OK );
          176  +  assert( rc!=SQLITE_OK || nRec>0 );
   176    177     pIter->iReadOff += iOff+nRec;
   177    178     pIter->nKey = nRec;
   178    179     pIter->aKey = &pIter->aAlloc[iOff];
   179    180     return rc;
   180    181   }
   181    182   
   182    183   /*
................................................................................
   539    540   ){
   540    541     VdbeSorter *pSorter = pCsr->pSorter;
   541    542     int rc = SQLITE_OK;             /* Return code */
   542    543     int i;                          /* Used to iterator through aIter[] */
   543    544     i64 nByte = 0;                  /* Total bytes in all opened PMAs */
   544    545   
   545    546     /* Initialize the iterators. */
   546         -  for(i=0; rc==SQLITE_OK && i<SORTER_MAX_MERGE_COUNT; i++){
          547  +  for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
   547    548       VdbeSorterIter *pIter = &pSorter->aIter[i];
   548    549       rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
   549    550       pSorter->iReadOff = pIter->iEof;
   550         -    assert( pSorter->iReadOff<=pSorter->iWriteOff || rc!=SQLITE_OK );
   551         -    if( pSorter->iReadOff>=pSorter->iWriteOff ) break;
          551  +    assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
          552  +    if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
   552    553     }
   553    554   
   554    555     /* Initialize the aTree[] array. */
   555    556     for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
   556    557       rc = vdbeSorterDoCompare(pCsr, i);
   557    558     }
   558    559