/ Check-in [0f7fdb02]
Login

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

Overview
Comment:Fix an memory allocation error revealed by malloc3.test. (CVS 3733)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0f7fdb022ca7c94f7d264192e18b6e2bd1e8cff4
User & Date: drh 2007-03-28 01:59:34
Context
2007-03-28
13:07
Update comments in sqlite3.h. No changes to code. (CVS 3734) check-in: 1c2656fd user: drh tags: trunk
01:59
Fix an memory allocation error revealed by malloc3.test. (CVS 3733) check-in: 0f7fdb02 user: drh tags: trunk
2007-03-27
22:24
The SQLITE_ENABLE_LOAD_EXTENSION macro enables the load_extension() SQL function by default without having to invoke sqlite3_enable_load_extension() first. (CVS 3732) check-in: 113aab2c user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

    14     14   ** The pager is used to access a database disk file.  It implements
    15     15   ** atomic commit and rollback through the use of a journal file that
    16     16   ** is separate from the database file.  The pager also implements file
    17     17   ** locking to prevent two processes from writing the same database
    18     18   ** file simultaneously, or one process from reading the database while
    19     19   ** another is writing.
    20     20   **
    21         -** @(#) $Id: pager.c,v 1.303 2007/03/27 17:37:32 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.304 2007/03/28 01:59:34 drh Exp $
    22     22   */
    23     23   #ifndef SQLITE_OMIT_DISKIO
    24     24   #include "sqliteInt.h"
    25     25   #include "os.h"
    26     26   #include "pager.h"
    27     27   #include <assert.h>
    28     28   #include <string.h>
................................................................................
  3056   3056     }
  3057   3057     rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,
  3058   3058                                    pPager->tempFile);
  3059   3059     pPager->journalOff = 0;
  3060   3060     pPager->setMaster = 0;
  3061   3061     pPager->journalHdr = 0;
  3062   3062     if( rc!=SQLITE_OK ){
         3063  +    if( rc==SQLITE_NOMEM ){
         3064  +      sqlite3OsDelete(pPager->zJournal);
         3065  +    }
  3063   3066       goto failed_to_open_journal;
  3064   3067     }
  3065   3068     sqlite3OsSetFullSync(pPager->jfd, pPager->full_fsync);
  3066   3069     sqlite3OsSetFullSync(pPager->fd, pPager->full_fsync);
  3067   3070     sqlite3OsOpenDirectory(pPager->jfd, pPager->zDirectory);
  3068   3071     pPager->journalOpen = 1;
  3069   3072     pPager->journalStarted = 0;
................................................................................
  3088   3091       }
  3089   3092     }
  3090   3093     return rc;
  3091   3094   
  3092   3095   failed_to_open_journal:
  3093   3096     sqliteFree(pPager->aInJournal);
  3094   3097     pPager->aInJournal = 0;
         3098  +#if 0
  3095   3099     if( rc==SQLITE_NOMEM ){
  3096   3100       /* If this was a malloc() failure, then we will not be closing the pager
  3097   3101       ** file. So delete any journal file we may have just created. Otherwise,
  3098   3102       ** the system will get confused, we have a read-lock on the file and a
  3099   3103       ** mysterious journal has appeared in the filesystem.
  3100   3104       */
  3101   3105       /* sqlite3OsDelete(pPager->zJournal); */
  3102   3106     }else{
  3103         -    pager_reset(pPager);
         3107  +    /* If we reset the pager here, we will delete pages out from under
         3108  +    ** various cursors and will ultimately segfault. */
         3109  +    /* pager_reset(pPager); */
  3104   3110     }
         3111  +#endif
  3105   3112     return rc;
  3106   3113   }
  3107   3114   
  3108   3115   /*
  3109   3116   ** Acquire a write-lock on the database.  The lock is removed when
  3110   3117   ** the any of the following happen:
  3111   3118   **

Changes to test/malloc3.test.

     9      9   #
    10     10   #***********************************************************************
    11     11   #
    12     12   # This file contains tests to ensure that the library handles malloc() failures
    13     13   # correctly. The emphasis of these tests are the _prepare(), _step() and
    14     14   # _finalize() calls.
    15     15   #
    16         -# $Id: malloc3.test,v 1.9 2006/01/23 07:52:41 danielk1977 Exp $
           16  +# $Id: malloc3.test,v 1.10 2007/03/28 01:59:34 drh Exp $
    17     17   
    18     18   set testdir [file dirname $argv0]
    19     19   source $testdir/tester.tcl
    20     20   
    21     21   # Only run these tests if memory debugging is turned on.
    22     22   if {[info command sqlite_malloc_stat]==""} {
    23     23      puts "Skipping malloc tests: not compiled with -DSQLITE_MEMDEBUG..."
................................................................................
   523    523       error "Uneven number of arguments to TEST"
   524    524     }
   525    525   
   526    526     for {set i 0} {$i < $pcstart} {incr i} {
   527    527       set k2 [lindex $arglist [expr 2 * $i]]
   528    528       set v2 [lindex $arglist [expr 2 * $i + 1]]
   529    529       set ac [sqlite3_get_autocommit $::DB]        ;# Auto-Commit
   530         -# puts "STARTUP"
   531    530       switch -- $k2 {
   532    531         -sql  {db eval [lindex $v2 1]}
   533    532         -prep {db eval $v2}
   534    533       }
   535    534       set nac [sqlite3_get_autocommit $::DB]       ;# New Auto-Commit 
   536    535       if {$ac && !$nac} {set begin_pc $i}
   537    536     }
................................................................................
   540    539   
   541    540     set iFail $iFailStart
   542    541     set pc $pcstart
   543    542     while {$pc*2 < [llength $arglist]} {
   544    543   
   545    544       # Id of this iteration:
   546    545       set iterid "(pc $pc).(iFail $iFail)"
   547         -
   548    546       set k [lindex $arglist [expr 2 * $pc]]
   549    547       set v [lindex $arglist [expr 2 * $pc + 1]]
   550    548   
   551    549       switch -- $k {
   552    550   
   553    551         -test { 
   554    552           foreach {id script} $v {}
................................................................................
   558    556         }
   559    557   
   560    558         -sql {
   561    559           set ::rollback_hook_count 0
   562    560   
   563    561           set ac [sqlite3_get_autocommit $::DB]        ;# Auto-Commit
   564    562           sqlite_malloc_fail $iFail
   565         -# puts "SQL $iterid [lindex $v 1]"
   566    563           set rc [catch {db eval [lindex $v 1]} msg]   ;# True error occurs
   567         -# puts "rc = $rc msg = \"$msg\""
   568    564           set nac [sqlite3_get_autocommit $::DB]       ;# New Auto-Commit 
   569    565   
   570    566   
   571    567           if {$rc != 0 && $nac && !$ac} {
   572    568             # Before [db eval] the auto-commit flag was clear. Now it
   573    569             # is set. Since an error occured we assume this was not a
   574    570   	  # commit - therefore a rollback occured. Check that the
................................................................................
   603    599               incr iFail
   604    600               if {$nac && !$ac} {
   605    601   
   606    602                 if {![lindex $v 0]} {
   607    603                   error "Statement \"[lindex $v 1]\" caused a rollback"
   608    604                 }
   609    605   
   610         -# puts "Statement \"[lindex $v 1]\" caused a rollback"
   611    606                 for {set i $begin_pc} {$i < $pc} {incr i} {
   612    607                   set k2 [lindex $arglist [expr 2 * $i]]
   613    608                   set v2 [lindex $arglist [expr 2 * $i + 1]]
   614    609                   set catchupsql ""
   615    610                   switch -- $k2 {
   616    611                     -sql  {set catchupsql [lindex $v2 1]}
   617    612                     -prep {set catchupsql $v2}
   618    613                   }
   619         -# puts "CATCHUP $iterid $i $catchupsql"
   620    614                   db eval $catchupsql
   621    615                 }
   622    616               }
   623    617           } else {
   624    618               error $msg
   625    619           }
   626    620   
   627    621           while {[lindex $arglist [expr 2 * ($pc -1)]] == "-test"} {
   628    622             incr pc -1
   629    623           }
   630    624         }
   631    625   
   632    626         -prep {
   633         -# puts "PREP $iterid $v"
   634    627           db eval $v
   635    628           incr pc
   636    629         }
   637    630   
   638    631         default { error "Unknown switch: $k" }
   639    632       }
   640         -# if {$iFail > ($iFailStart+1)} return
   641    633     }
   642    634   }
   643    635   
   644    636   # Turn of the Tcl interface's prepared statement caching facility.
   645    637   db cache size 0
   646    638   
   647    639   run_test $::run_test_script 9 1
................................................................................
   648    640   # run_test [lrange $::run_test_script 0 3] 0 63
   649    641   sqlite_malloc_fail 0
   650    642   db close
   651    643   
   652    644   pp_check_for_leaks
   653    645   
   654    646   finish_test
   655         -