/ Check-in [10648e99]
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:When the write_version flag in the database header is larger than what the library understands, make the database read-only. The old behavior was to make the database unreadable. (CVS 3866)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 10648e99929b4f640855433b6e47702687039286
User & Date: drh 2007-04-24 17:27:51
Context
2007-04-24
17:35
This fixes a missed case in check-in (3866). Do not apply patch (3866) without also applying this patch. (CVS 3867) check-in: 66c2fa08 user: drh tags: trunk
17:27
When the write_version flag in the database header is larger than what the library understands, make the database read-only. The old behavior was to make the database unreadable. (CVS 3866) check-in: 10648e99 user: drh tags: trunk
2007-04-23
23:56
Get the build working with -DSQLITE_OMIT_UTF16=1. Ticket #2314. (CVS 3865) check-in: 25c1c7aa user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/btree.c.

     5      5   ** a legal notice, here is a blessing:
     6      6   **
     7      7   **    May you do good and not evil.
     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12         -** $Id: btree.c,v 1.356 2007/04/19 00:24:34 drh Exp $
           12  +** $Id: btree.c,v 1.357 2007/04/24 17:27:51 drh Exp $
    13     13   **
    14     14   ** This file implements a external (disk-based) database using BTrees.
    15     15   ** For a detailed discussion of BTrees, refer to
    16     16   **
    17     17   **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
    18     18   **     "Sorting And Searching", pages 473-480. Addison-Wesley
    19     19   **     Publishing Company, Reading, Massachusetts.
................................................................................
  1866   1866     */
  1867   1867     rc = SQLITE_NOTADB;
  1868   1868     if( sqlite3PagerPagecount(pBt->pPager)>0 ){
  1869   1869       u8 *page1 = pPage1->aData;
  1870   1870       if( memcmp(page1, zMagicHeader, 16)!=0 ){
  1871   1871         goto page1_init_failed;
  1872   1872       }
  1873         -    if( page1[18]>1 || page1[19]>1 ){
         1873  +    if( page1[18]>1 ){
         1874  +      pBt->readOnly = 1;
         1875  +    }
         1876  +    if( page1[19]>1 ){
  1874   1877         goto page1_init_failed;
  1875   1878       }
  1876   1879       pageSize = get2byte(&page1[16]);
  1877   1880       if( ((pageSize-1)&pageSize)!=0 || pageSize<512 ){
  1878   1881         goto page1_init_failed;
  1879   1882       }
  1880   1883       assert( (pageSize & 7)==0 );
................................................................................
  2066   2069   
  2067   2070     do {
  2068   2071       if( pBt->pPage1==0 ){
  2069   2072         rc = lockBtree(pBt);
  2070   2073       }
  2071   2074     
  2072   2075       if( rc==SQLITE_OK && wrflag ){
         2076  +      if( pBt->readOnly ){
         2077  +        rc = SQLITE_READONLY;
         2078  +      }else{
  2073   2079         rc = sqlite3PagerBegin(pBt->pPage1->pDbPage, wrflag>1);
  2074   2080         if( rc==SQLITE_OK ){
  2075   2081           rc = newDatabase(pBt);
  2076   2082         }
  2077   2083       }
         2084  +    }
  2078   2085     
  2079   2086       if( rc==SQLITE_OK ){
  2080   2087         if( wrflag ) pBt->inStmt = 0;
  2081   2088       }else{
  2082   2089         unlockBtreeIfUnused(pBt);
  2083   2090       }
  2084   2091     }while( rc==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&

Added test/rdonly.test.

            1  +# 2007 April 24
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements regression tests for SQLite library.
           12  +#
           13  +# This file implements tests to make sure SQLite treats a database
           14  +# as readonly if its write version is set to  high.
           15  +#
           16  +# $Id: rdonly.test,v 1.1 2007/04/24 17:27:52 drh Exp $
           17  +
           18  +set testdir [file dirname $argv0]
           19  +source $testdir/tester.tcl
           20  +
           21  +
           22  +# Create a database.
           23  +#
           24  +do_test rdonly-1.1 {
           25  +  execsql {
           26  +    CREATE TABLE t1(x);
           27  +    INSERT INTO t1 VALUES(1);
           28  +    SELECT * FROM t1;
           29  +  }
           30  +} {1}
           31  +
           32  +# Changes the write version from 1 to 2.  Verify that the database
           33  +# can be read but not written.
           34  +#
           35  +do_test rdonly-1.2 {
           36  +  db close
           37  +  hexio_get_int [hexio_read test.db 18 1]
           38  +} 1
           39  +do_test rdonly-1.3 {
           40  +  hexio_write test.db 18 02
           41  +  sqlite3 db test.db
           42  +  execsql {
           43  +    SELECT * FROM t1;
           44  +  }
           45  +} {1}
           46  +do_test rdonly-1.4 {
           47  +  catchsql {
           48  +    INSERT INTO t1 VALUES(2)
           49  +  }
           50  +} {1 {attempt to write a readonly database}}
           51  +
           52  +# Change the write version back to 1.  Verify that the database
           53  +# is read-write again.
           54  +#
           55  +do_test rdonly-1.5 {
           56  +  db close
           57  +  hexio_write test.db 18 01
           58  +  sqlite3 db test.db
           59  +  catchsql {
           60  +    INSERT INTO t1 VALUES(2);
           61  +    SELECT * FROM t1;
           62  +  }
           63  +} {0 {1 2}}
           64  +
           65  +finish_test