From 8067adbc3f17ba9eb3d71f88efe29f31717a40c5 Mon Sep 17 00:00:00 2001
From: dan <dan@noemail.net>
Date: Fri, 21 May 2010 16:23:43 +0000
Subject: [PATCH] Correct an off-by-one bug in the previous commit.

FossilOrigin-Name: 75a1130d86faf18e73cb46cda5d029f1e913e4e6
---
 manifest           | 16 ++++++++--------
 manifest.uuid      |  2 +-
 src/wal.c          |  2 +-
 test/wal2.test     | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 test/walfault.test | 18 +++++++++++++++++-
 5 files changed, 72 insertions(+), 12 deletions(-)

diff --git a/manifest b/manifest
index 4e7dd7e25f..b5c7007617 100644
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Remove\sentries\sfrom\swal-index\shash\stables\swhen\sa\srollback\sor\ssavepoint\srollback\soccurs.
-D 2010-05-21T15:31:57
+C Correct\san\soff-by-one\sbug\sin\sthe\sprevious\scommit.
+D 2010-05-21T16:23:44
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in a5cad1f8f3e021356bfcc6c77dc16f6f1952bbc3
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -224,7 +224,7 @@ F src/vdbeblob.c 5327132a42a91e8b7acfb60b9d2c3b1c5c863e0e
 F src/vdbemem.c 2a82f455f6ca6f78b59fb312f96054c04ae0ead1
 F src/vdbetrace.c 864cef96919323482ebd9986f2132435115e9cc2
 F src/vtab.c a0f8a40274e4261696ef57aa806de2776ab72cda
-F src/wal.c c05f4091810a4cf4ff0e87f667c6e6341e143516
+F src/wal.c 2b984834bb8f235919c2b476c03dcb2e7e5b83d6
 F src/wal.h 434f76f51225bb614e43ccb6bd2341541ba6a06e
 F src/walker.c 3112bb3afe1d85dc52317cb1d752055e9a781f8f
 F src/where.c 75fee9e255b62f773fcadd1d1f25b6f63ac7a356
@@ -762,10 +762,10 @@ F test/vtab_alter.test 9e374885248f69e251bdaacf480b04a197f125e5
 F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8
 F test/vtab_shared.test 0eff9ce4f19facbe0a3e693f6c14b80711a4222d
 F test/wal.test 90afd254ece957a716751b1c35fac02d6353c2a7
-F test/wal2.test 6033cec945f51e3fc19502a69995aecb4252ca03
+F test/wal2.test 053c9ea94194c5bce5b742429be75ff2432794ab
 F test/walbak.test e7650a26eb4b8abeca9b145b1af1e63026dde432
 F test/walcrash.test f6d5fb2bb108876f04848720a488065d9deef69f
-F test/walfault.test 98df47444944a6db2161eed5cef71d6c00bcb8c3
+F test/walfault.test f71d4c9a13d4e27086aef55f1e0e94734ffa2f6a
 F test/walhook.test 67e675127f4acb72f061a12667ce6e5460b06b78
 F test/walmode.test 6ca9d710cc9f6545b913abcded6d6b0b15641048
 F test/walslow.test d21625e2e99e11c032ce949e8a94661576548933
@@ -813,7 +813,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
 F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f
-P 1a4eb3a3efe86c7caff4d9a5894953bce378f841
-R 8ca2ea872b35ad91571755768a000a84
+P 36795c2b23a78978528cace193e386138adacd41
+R 63c928b9ccf2140d57615bbbd5e376ff
 U dan
-Z 71ea7d7c3c51659a819f7c811c09c6cb
+Z ffdf9334b2c259a667ae34031d21c40b
diff --git a/manifest.uuid b/manifest.uuid
index b080f4751c..a8ae2d038b 100644
--- a/manifest.uuid
+++ b/manifest.uuid
@@ -1 +1 @@
-36795c2b23a78978528cace193e386138adacd41
\ No newline at end of file
+75a1130d86faf18e73cb46cda5d029f1e913e4e6
\ No newline at end of file
diff --git a/src/wal.c b/src/wal.c
index d6e712d531..82a1c212de 100644
--- a/src/wal.c
+++ b/src/wal.c
@@ -1491,7 +1491,7 @@ static void walClearHash(Wal *pWal, u32 iOldMx, u32 iNewMx){
     iLimit = iNewMx - iZero;
     if( iLimit>0 ){
       int i;                      /* Used to iterate through aHash[] */
-      for(i=1; i<=HASHTABLE_NPAGE; i++){
+      for(i=0; i<HASHTABLE_NPAGE; i++){
         if( aHash[i]>iLimit ){
           aHash[i] = 0;
         }
diff --git a/test/wal2.test b/test/wal2.test
index ece7543eb8..1e93818046 100644
--- a/test/wal2.test
+++ b/test/wal2.test
@@ -115,7 +115,6 @@ foreach {tn iInsert res wal_index_hdr_mod wal_locks} {
       return SQLITE_OK
     }
 
-breakpoint
     execsql { SELECT count(a), sum(a) FROM t1 } db2
   } $res
 
@@ -605,6 +604,7 @@ db close
 #-------------------------------------------------------------------------
 # Test a theory about the checksum algorithm. Theory was false and this
 # test did not provoke a bug.
+#
 file delete -force test.db test.db-wal test.db-journal
 do_test wal2-7.1.1 {
   sqlite3 db test.db
@@ -627,5 +627,49 @@ do_test wal2-7.1.3 {
 } {}
 db2 close
 
+file delete -force test.db test.db-wal test.db-journal
+do_test wal2-8.1.2 {
+  sqlite3 db test.db
+  execsql {
+    PRAGMA page_size = 1024;
+    PRAGMA journal_mode = WAL;
+    CREATE TABLE t1(x);
+    INSERT INTO t1 VALUES(zeroblob(8188*1020));
+    CREATE TABLE t2(y);
+  }
+  execsql {
+    PRAGMA wal_checkpoint;
+    SELECT rootpage FROM sqlite_master WHERE tbl_name = 't2';
+  }
+} {8192}
+do_test wal2-8.1.3 {
+  execsql {
+    PRAGMA cache_size = 10;
+    CREATE TABLE t3(z);
+    BEGIN;
+      INSERT INTO t3 VALUES(randomblob(900));
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+      INSERT INTO t2 VALUES('hello');
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+      INSERT INTO t3 SELECT randomblob(900) FROM t3;
+    ROLLBACK;
+  }
+  execsql {
+    INSERT INTO t2 VALUES('goodbye');
+    INSERT INTO t3 SELECT randomblob(900) FROM t3;
+    INSERT INTO t3 SELECT randomblob(900) FROM t3;
+  }
+} {}
+do_test wal2-8.1.4 {
+  sqlite3 db2 test.db
+  execsql { SELECT * FROM t2 }
+} {goodbye}
+db2 close
+
+
 finish_test
 
diff --git a/test/walfault.test b/test/walfault.test
index e542e11281..9d41e50fd5 100644
--- a/test/walfault.test
+++ b/test/walfault.test
@@ -237,7 +237,7 @@ do_shmfault_test walfault-shm-4 -tclprep {
   SELECT count(*) FROM t1;
 }
 
-do_shmfault_test walfault-shm-5 -coverageonly 1 -sqlprep {
+do_shmfault_test walfault-shm-5.1 -coverageonly 1 -sqlprep {
   PRAGMA cache_size = 10;
   PRAGMA journal_mode = WAL;
   CREATE TABLE abc(a PRIMARY KEY);
@@ -251,6 +251,22 @@ do_shmfault_test walfault-shm-5 -coverageonly 1 -sqlprep {
   ROLLBACK;
 }
 
+do_shmfault_test walfault-shm-5.2 -coverageonly 1 -sqlprep {
+  PRAGMA cache_size = 10;
+  PRAGMA journal_mode = WAL;
+  CREATE TABLE abc(a PRIMARY KEY);
+  INSERT INTO abc VALUES(randomblob(900));
+} -sqlbody {
+  BEGIN;
+    INSERT INTO abc SELECT randomblob(900) FROM abc;    /* 1 */
+    SAVEPOINT spoint;
+      INSERT INTO abc SELECT randomblob(900) FROM abc;    /* 2 */
+      INSERT INTO abc SELECT randomblob(900) FROM abc;    /* 4 */
+      INSERT INTO abc SELECT randomblob(900) FROM abc;    /* 8 */
+    ROLLBACK TO spoint;
+  COMMIT;
+}
+
 #-------------------------------------------------------------------------
 # When a database is checkpointed, SQLite does the following:
 #