/ Check-in [3c8512bc]
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:Fix a problem where a file was not being closed after a malloc() failure. (CVS 1741)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3c8512bc549e10ee131cb7f2d4e74d96e9de74a0
User & Date: danielk1977 2004-06-26 13:51:34
Context
2004-06-26
14:46
Fold in a 64-bit change that came from the PHP people. (CVS 1743) check-in: b69b4fe8 user: drh tags: trunk
13:51
Fix a problem where a file was not being closed after a malloc() failure. (CVS 1741) check-in: 3c8512bc user: danielk1977 tags: trunk
12:40
Add news about the break-in to the homepage. (CVS 1740) check-in: 5cda1cda 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.144 2004/06/26 08:38:25 danielk1977 Exp $
           21  +** @(#) $Id: pager.c,v 1.145 2004/06/26 13:51:34 danielk1977 Exp $
    22     22   */
    23     23   #include "os.h"         /* Must be first to enable large file support */
    24     24   #include "sqliteInt.h"
    25     25   #include "pager.h"
    26     26   #include <assert.h>
    27     27   #include <string.h>
    28     28   
................................................................................
  1431   1431       zFilename = zTemp;
  1432   1432       zFullPathname = sqlite3OsFullPathname(zFilename);
  1433   1433       if( rc==SQLITE_OK ){
  1434   1434         tempFile = 1;
  1435   1435       }
  1436   1436     }
  1437   1437     if( !zFullPathname ){
         1438  +    sqlite3OsClose(&fd);
  1438   1439       return SQLITE_NOMEM;
  1439   1440     }
  1440   1441     if( rc!=SQLITE_OK ){
  1441   1442       sqlite3OsClose(&fd);
  1442   1443       sqliteFree(zFullPathname);
  1443   1444       return rc;
  1444   1445     }

Changes to src/vdbeaux.c.

   122    122   ** the label is resolved to a specific address, the VDBE will scan
   123    123   ** through its operation list and change all values of P2 which match
   124    124   ** the label into the resolved address.
   125    125   **
   126    126   ** The VDBE knows that a P2 value is a label because labels are
   127    127   ** always negative and P2 values are suppose to be non-negative.
   128    128   ** Hence, a negative P2 value is a label that has yet to be resolved.
          129  +**
          130  +** Zero is returned if a malloc() fails.
   129    131   */
   130    132   int sqlite3VdbeMakeLabel(Vdbe *p){
   131    133     int i;
   132    134     i = p->nLabel++;
   133    135     assert( p->magic==VDBE_MAGIC_INIT );
   134    136     if( i>=p->nLabelAlloc ){
   135    137       int *aNew;
................................................................................
  1305   1307   }
  1306   1308   
  1307   1309   /*
  1308   1310   ** Clean up and delete a VDBE after execution.  Return an integer which is
  1309   1311   ** the result code.  Write any error message text into *pzErrMsg.
  1310   1312   */
  1311   1313   int sqlite3VdbeFinalize(Vdbe *p){
  1312         -  int rc;
         1314  +  int rc = SQLITE_OK;
  1313   1315     sqlite *db;
  1314   1316   
  1315         -  if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
  1316         -    if( p->magic==VDBE_MAGIC_INIT ){
  1317         -      sqlite3Error(p->db, SQLITE_MISUSE, 0);
  1318         -    }
         1317  +  if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
         1318  +    rc = sqlite3VdbeReset(p);
         1319  +  }else if( p->magic!=VDBE_MAGIC_INIT ){
         1320  +    /* sqlite3Error(p->db, SQLITE_MISUSE, 0); */
  1319   1321       return SQLITE_MISUSE;
  1320   1322     }
  1321         -  db = p->db;
  1322         -  rc = sqlite3VdbeReset(p);
  1323   1323     sqlite3VdbeDelete(p);
  1324   1324     if( rc==SQLITE_SCHEMA ){
  1325   1325       sqlite3ResetInternalSchema(db, 0);
  1326   1326     }
  1327   1327     return rc;
  1328   1328   }
  1329   1329   

Changes to test/malloc.test.

    10     10   #***********************************************************************
    11     11   # This file attempts to check the library in an out-of-memory situation.
    12     12   # When compiled with -DMEMORY_DEBUG=1, the SQLite library accepts a special
    13     13   # command (sqlite_malloc_fail N) which causes the N-th malloc to fail.  This
    14     14   # special feature is used to see what happens in the library if a malloc
    15     15   # were to really fail due to an out-of-memory situation.
    16     16   #
    17         -# $Id: malloc.test,v 1.8 2004/06/19 00:16:31 drh Exp $
           17  +# $Id: malloc.test,v 1.9 2004/06/26 13:51:34 danielk1977 Exp $
    18     18   
    19     19   set testdir [file dirname $argv0]
    20     20   source $testdir/tester.tcl
    21     21   
    22     22   # Only run these tests if memory debugging is turned on.
    23     23   #
    24     24   if {[info command sqlite_malloc_stat]==""} {
................................................................................
    60     60        } else {
    61     61          set v2 [expr {$msg=="" || $msg=="out of memory"}]
    62     62          if {!$v2} {puts "\nError message returned: $msg"}
    63     63          lappend v $v2
    64     64        }
    65     65     } {1 1}
    66     66   }
           67  +
           68  +# Ensure that no file descriptors were leaked.
           69  +do_test malloc-1.X {
           70  +  catch {db close}
           71  +  set sqlite_open_file_count
           72  +} {0}
    67     73   
    68     74   set fd [open ./data.tmp w]
    69     75   for {set i 1} {$i<=20} {incr i} {
    70     76     puts $fd "$i\t[expr {$i*$i}]\t[expr {100-$i}]  abcdefghijklmnopqrstuvwxyz"
    71     77   }
    72     78   close $fd
    73     79   
................................................................................
   110    116        } else {
   111    117          set v2 [expr {$msg=="" || $msg=="out of memory"}]
   112    118          if {!$v2} {puts "\nError message returned: $msg"}
   113    119          lappend v $v2
   114    120        }
   115    121     } {1 1}
   116    122   }
          123  +
          124  +# Ensure that no file descriptors were leaked.
          125  +do_test malloc-2.X {
          126  +  catch {db close}
          127  +  set sqlite_open_file_count
          128  +} {0}
   117    129   
   118    130   for {set go 1; set i 1} {$go} {incr i} {
   119    131     do_test malloc-3.$i {
   120    132        sqlite_malloc_fail 0
   121    133        catch {db close}
   122    134        catch {file delete -force test.db}
   123    135        catch {file delete -force test.db-journal}
................................................................................
   151    163        } else {
   152    164          set v2 [expr {$msg=="" || $msg=="out of memory"}]
   153    165          if {!$v2} {puts "\nError message returned: $msg"}
   154    166          lappend v $v2
   155    167        }
   156    168     } {1 1}
   157    169   }
          170  +
          171  +# Ensure that no file descriptors were leaked.
          172  +do_test malloc-3.X {
          173  +  catch {db close}
          174  +  set sqlite_open_file_count
          175  +} {0}
          176  +
   158    177   for {set go 1; set i 1} {$go} {incr i} {
   159    178     do_test malloc-4.$i {
   160    179        sqlite_malloc_fail 0
   161    180        catch {db close}
   162    181        catch {file delete -force test.db}
   163    182        catch {file delete -force test.db-journal}
   164    183        sqlite_malloc_fail $i
................................................................................
   192    211        } else {
   193    212          set v2 [expr {$msg=="" || $msg=="out of memory"}]
   194    213          if {!$v2} {puts "\nError message returned: $msg"}
   195    214          lappend v $v2
   196    215        }
   197    216     } {1 1}
   198    217   }
          218  +
          219  +# Ensure that no file descriptors were leaked.
          220  +do_test malloc-4.X {
          221  +  catch {db close}
          222  +  set sqlite_open_file_count
          223  +} {0}
          224  +
   199    225   for {set go 1; set i 1} {$go} {incr i} {
   200    226     do_test malloc-5.$i {
   201    227        sqlite_malloc_fail 0
   202    228        catch {db close}
   203    229        catch {file delete -force test.db}
   204    230        catch {file delete -force test.db-journal}
   205    231        sqlite_malloc_fail $i
................................................................................
   226    252        } else {
   227    253          set v2 [expr {$msg=="" || $msg=="out of memory"}]
   228    254          if {!$v2} {puts "\nError message returned: $msg"}
   229    255          lappend v $v2
   230    256        }
   231    257     } {1 1}
   232    258   }
          259  +
          260  +# Ensure that no file descriptors were leaked.
          261  +do_test malloc-5.X {
          262  +  catch {db close}
          263  +  set sqlite_open_file_count
          264  +} {0}
          265  +
   233    266   sqlite_malloc_fail 0
   234    267   finish_test