]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Reformat WAL code for clearer presentation. Update comments for correctness.
authordrh <drh@noemail.net>
Fri, 9 Jul 2010 03:19:07 +0000 (03:19 +0000)
committerdrh <drh@noemail.net>
Fri, 9 Jul 2010 03:19:07 +0000 (03:19 +0000)
Add checks to ensure that corruption in shared-memory does not result in
an infinite loop.

FossilOrigin-Name: 40eaada7ec45e70bdf64d060051f24c5c5e3faf3

manifest
manifest.uuid
src/wal.c

index b623c0d4098373666ce3c1e729381dd2a5dd277a..2a59ea72e8bad9c7fb0227e40fd75cbd773f8995 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,8 +1,8 @@
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
-C Add\sa\stest\scase\sto\scover\scode\sthat\swas\sleft\suntested\sby\sthe\sjournal_mode\nsimplification.
-D 2010-07-08T19:19:51
+C Reformat\sWAL\scode\sfor\sclearer\spresentation.\s\sUpdate\scomments\sfor\scorrectness.\nAdd\schecks\sto\sensure\sthat\scorruption\sin\sshared-memory\sdoes\snot\sresult\sin\nan\sinfinite\sloop.
+D 2010-07-09T03:19:07
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in c4270a1cd7cd70a263b7e96a258aa90e9c3618eb
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -229,7 +229,7 @@ F src/vdbeblob.c 258a6010ba7a82b72b327fb24c55790655689256
 F src/vdbemem.c 5e579abf6532001dfbee0e640dc34eae897a9807
 F src/vdbetrace.c 864cef96919323482ebd9986f2132435115e9cc2
 F src/vtab.c a0f8a40274e4261696ef57aa806de2776ab72cda
-F src/wal.c 5a58352aee4fb0546f4b94f8527a06300531bae9
+F src/wal.c 0cd5cd46df9237671def76abaf8152af4bd4128e
 F src/wal.h 906c85760598b18584921fe08008435aa4eeeeb2
 F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f
 F src/where.c 926c83c6394e132a1c62b6b12ceeba7d55a34c19
@@ -835,14 +835,14 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P aef26438523bccf1ea2c19205cd05799e4c69a1f
-R 95f7318feaa66a8b78205d887e6d1438
+P bcdddba4f011cf008d29918bcac59131007a8138
+R 5e19b5fcbb0a4bb30775e5e30106627d
 U drh
-Z 04e790b790d53c3d8a934d95e1981b36
+Z 4cec72d41693a1e955d49c74ad6f997b
 -----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1.4.6 (GNU/Linux)
 
-iD8DBQFMNiTboxKgR168RlERAkDBAJ0RhukLRTBMs1DtkngKJab4oKQ5ywCgg48P
-LJSpxRhRYLMurIbiJuDHwuo=
-=eiHk
+iD8DBQFMNpUuoxKgR168RlERAq/pAJ97NzUyxw7J6MN7JJON213vZ349xQCeOtGo
+gYdze9fjpunjGvwfhDWNebg=
+=jEhp
 -----END PGP SIGNATURE-----
index 8488a6ca3f5f470b0f9dd0efa31b3855a3bb1d3e..d933239293fa87760480291431397eac9e1e60ad 100644 (file)
@@ -1 +1 @@
-bcdddba4f011cf008d29918bcac59131007a8138
\ No newline at end of file
+40eaada7ec45e70bdf64d060051f24c5c5e3faf3
\ No newline at end of file
index 89cc9698074ef6414b2a98e8393c74e0309e3348..3a8782177256b04caa9ab5862e54698559bf4c2c 100644 (file)
--- a/src/wal.c
+++ b/src/wal.c
@@ -495,14 +495,15 @@ static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
 
   /* Enlarge the pWal->apWiData[] array if required */
   if( pWal->nWiData<=iPage ){
-    int nByte = sizeof(u32 *)*(iPage+1);
+    int nByte = sizeof(u32*)*(iPage+1);
     volatile u32 **apNew;
     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
     if( !apNew ){
       *ppPage = 0;
       return SQLITE_NOMEM;
     }
-    memset((void *)&apNew[pWal->nWiData], 0, sizeof(u32 *)*(iPage+1-pWal->nWiData));
+    memset((void*)&apNew[pWal->nWiData], 0,
+           sizeof(u32*)*(iPage+1-pWal->nWiData));
     pWal->apWiData = apNew;
     pWal->nWiData = iPage+1;
   }
@@ -678,9 +679,10 @@ static int walDecodeFrame(
     return 0;
   }
 
-  /* A frame is only valid if a checksum of the first 16 bytes
-  ** of the frame-header, and the frame-data matches
-  ** the checksum in the last 8 bytes of the frame-header.
+  /* A frame is only valid if a checksum of the WAL header,
+  ** all prior frams, the first 16 bytes of this frame-header, 
+  ** and the frame-data matches the checksum in the last 8 
+  ** bytes of this frame-header.
   */
   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
@@ -940,7 +942,7 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
   if( rc==SQLITE_OK ){
     int iKey;                     /* Hash table key */
     int idx;                      /* Value to write to hash-table slot */
-    TESTONLY( int nCollide = 0;   /* Number of hash collisions */ )
+    int nCollide;                 /* Number of hash collisions */
 
     idx = iFrame - iZero;
     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
@@ -965,8 +967,9 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
     }
 
     /* Write the aPgno[] array entry and the hash-table slot. */
+    nCollide = idx;
     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
-      assert( nCollide++ < idx );
+      if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
     }
     aPgno[idx] = iPage;
     aHash[iKey] = (ht_slot)idx;
@@ -1452,7 +1455,11 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){
       ht_slot *aIndex;            /* Sorted index for this segment */
 
       aPgno++;
-      nEntry = (int)(((i+1)==nSegment)?(int)(iLast-iZero):(u32 *)aHash-(u32 *)aPgno);
+      if( (i+1)==nSegment ){
+        nEntry = (int)(iLast - iZero);
+      }else{
+        nEntry = (u32*)aHash - (u32*)aPgno;
+      }
       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
       iZero++;
   
@@ -2089,18 +2096,23 @@ int sqlite3WalRead(
     volatile u32 *aPgno;          /* Pointer to array of page numbers */
     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
     int iKey;                     /* Hash slot index */
-    int rc;
+    int nCollide;                 /* Number of hash collisions remaining */
+    int rc;                       /* Error code */
 
     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
     if( rc!=SQLITE_OK ){
       return rc;
     }
+    nCollide = HASHTABLE_NSLOT;
     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
       u32 iFrame = aHash[iKey] + iZero;
       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
         assert( iFrame>iRead );
         iRead = iFrame;
       }
+      if( (nCollide--)==0 ){
+        return SQLITE_CORRUPT_BKPT;
+      }
     }
   }