SQLite

Check-in [ddf980a501]
Login

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

Overview
Comment:Fix the problems demonstrated in tkt35xx.test in a different way to (5936). (CVS 5938)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: ddf980a50127a9de35edeca5549f4b51e3f733e6
User & Date: danielk1977 2008-11-21 09:09:02.000
Context
2008-11-21
09:43
Fix an assert() failure that can occur after an OOM error. (CVS 5939) (check-in: 4c765758c1 user: danielk1977 tags: trunk)
09:09
Fix the problems demonstrated in tkt35xx.test in a different way to (5936). (CVS 5938) (check-in: ddf980a501 user: danielk1977 tags: trunk)
08:50
Add another test case to tkt35xx.test showing that a statement rollback can also trigger the problem. (CVS 5937) (check-in: 74c08b8dd9 user: danielk1977 tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/btree.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
** 2004 April 6
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** $Id: btree.c,v 1.539 2008/11/19 10:22:33 danielk1977 Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"












|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
** 2004 April 6
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** $Id: btree.c,v 1.540 2008/11/21 09:09:02 danielk1977 Exp $
**
** This file implements a external (disk-based) database using BTrees.
** See the header comment on "btreeInt.h" for additional information.
** Including a description of file format and an overview of operation.
*/
#include "btreeInt.h"

4300
4301
4302
4303
4304
4305
4306

4307
4308
4309


4310
4311
4312
4313
4314
4315
4316
  }

  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );

end_allocate_page:
  releasePage(pTrunk);
  releasePage(pPrevTrunk);

  if( rc==SQLITE_OK && sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
    releasePage(*ppPage);
    return SQLITE_CORRUPT_BKPT;


  }
  return rc;
}

/*
** Add a page of the database file to the freelist.
**







>
|
|
|
>
>







4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
  }

  assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );

end_allocate_page:
  releasePage(pTrunk);
  releasePage(pPrevTrunk);
  if( rc==SQLITE_OK ){
    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
      releasePage(*ppPage);
      return SQLITE_CORRUPT_BKPT;
    }
    (*ppPage)->isInit = 0;
  }
  return rc;
}

/*
** Add a page of the database file to the freelist.
**
Changes to src/pager.c.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
** The pager is used to access a database disk file.  It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file.  The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.507 2008/11/21 03:23:43 drh Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"

/*
** Macros for troubleshooting.  Normally turned off
*/







|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
** The pager is used to access a database disk file.  It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file.  The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.508 2008/11/21 09:09:02 danielk1977 Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"

/*
** Macros for troubleshooting.  Normally turned off
*/
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
      }
    }
  }
  /*NOTREACHED*/
  assert( 0 );

end_playback:
  /* There may be pages in cache that are dirty but which were not in
  ** the rollback journal.  Such pages would have been taken out of the
  ** freelist (and hence marked as DontRollback).  All such pages must
  ** be reinitialized.
  */
  if( rc==SQLITE_OK && pPager->xReiniter ){
    PgHdr *pDirty;     /* List of page that are still dirty */
    pDirty = sqlite3PcacheDirtyList(pPager->pPCache);
    while( pDirty ){
      pPager->xReiniter(pDirty);
      sqlite3PcacheMakeClean(pDirty);
      pDirty = pDirty->pDirty;
    }
  }
  if( rc==SQLITE_OK ){
    zMaster = pPager->pTmpSpace;
    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  }
  if( rc==SQLITE_OK ){
    rc = pager_end_transaction(pPager, zMaster[0]!='\0');
  }







<
<
<
<
<
<
<
<
<
<
<
<
<
<







1513
1514
1515
1516
1517
1518
1519














1520
1521
1522
1523
1524
1525
1526
      }
    }
  }
  /*NOTREACHED*/
  assert( 0 );

end_playback:














  if( rc==SQLITE_OK ){
    zMaster = pPager->pTmpSpace;
    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
  }
  if( rc==SQLITE_OK ){
    rc = pager_end_transaction(pPager, zMaster[0]!='\0');
  }
Changes to test/tkt35xx.test.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# When a transaction rolls back, make sure that dirty pages in the
# page cache which are not in the rollback journal are reinitialized
# in the btree layer.
#
# $Id: tkt35xx.test,v 1.2 2008/11/21 08:50:50 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_test tkt35xx-1.1 {
  execsql {
    PRAGMA auto_vacuum = 0;







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# When a transaction rolls back, make sure that dirty pages in the
# page cache which are not in the rollback journal are reinitialized
# in the btree layer.
#
# $Id: tkt35xx.test,v 1.3 2008/11/21 09:09:02 danielk1977 Exp $

set testdir [file dirname $argv0]
source $testdir/tester.tcl

do_test tkt35xx-1.1 {
  execsql {
    PRAGMA auto_vacuum = 0;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93






94
95
96
97
98
99
100
101
102
103
104
105
106
  catchsql {
    BEGIN;
    CREATE TABLE t5(e PRIMARY KEY, f);
    DROP TABLE t5;
    INSERT INTO t3(a, b) SELECT c, d FROM t4;
  }
} {1 {PRIMARY KEY must be unique}}

do_test tkt35xx-1.2.3 {
  # Show that the transaction has not been rolled back.
  catchsql BEGIN
} {1 {cannot start a transaction within a transaction}}

# Before the bug was fixed, if SQLITE_DEBUG was defined an assert()
# would fail during the following INSERT statement. If SQLITE_DEBUG
# was not defined, then the statement would pass and the transaction
# would be committed. But, the "SELECT count(*)" in tkt35xx-1.2.6 would
# return 1, not 5. Data magically disappeared!
#
do_test tkt35xx-1.2.4 {
  execsql { SELECT count(*) FROM t3 }
} {4}
do_test tkt35xx-1.2.5 {






  execsql { 
    INSERT INTO t3 VALUES(5, $big);
    COMMIT;
  }
} {}
do_test tkt35xx-1.2.6 {
  execsql { SELECT count(*) FROM t3 }
} {5}

integrity_check tkt35xx-1.2.7

finish_test








<




<
<
<
<
<
<
<




>
>
>
>
>
>








<




71
72
73
74
75
76
77

78
79
80
81







82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

100
101
102
103
  catchsql {
    BEGIN;
    CREATE TABLE t5(e PRIMARY KEY, f);
    DROP TABLE t5;
    INSERT INTO t3(a, b) SELECT c, d FROM t4;
  }
} {1 {PRIMARY KEY must be unique}}

do_test tkt35xx-1.2.3 {
  # Show that the transaction has not been rolled back.
  catchsql BEGIN
} {1 {cannot start a transaction within a transaction}}







do_test tkt35xx-1.2.4 {
  execsql { SELECT count(*) FROM t3 }
} {4}
do_test tkt35xx-1.2.5 {
  # Before the bug was fixed, if SQLITE_DEBUG was defined an assert()
  # would fail during the following INSERT statement. If SQLITE_DEBUG
  # was not defined, then the statement would pass and the transaction
  # would be committed. But, the "SELECT count(*)" in tkt35xx-1.2.6 would
  # return 1, not 5. Data magically disappeared!
  #
  execsql { 
    INSERT INTO t3 VALUES(5, $big);
    COMMIT;
  }
} {}
do_test tkt35xx-1.2.6 {
  execsql { SELECT count(*) FROM t3 }
} {5}

integrity_check tkt35xx-1.2.7

finish_test