SQLite

Check-in [3d413e9b46]
Login

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

Overview
Comment:Fix a problem in the test suite that could cause a crash if using a pre-allocated block of memory for pages (the problem was that sqlite3_shutdown() was being called while there were still open database connections). (CVS 5259)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3d413e9b466a871650597407016131df4d07b3d2
User & Date: danielk1977 2008-06-21 08:12:15.000
Context
2008-06-21
11:20
More documentation spellcheck and cleanup. No changes to code. (CVS 5260) (check-in: 8c457fb08b user: mihailim tags: trunk)
08:12
Fix a problem in the test suite that could cause a crash if using a pre-allocated block of memory for pages (the problem was that sqlite3_shutdown() was being called while there were still open database connections). (CVS 5259) (check-in: 3d413e9b46 user: danielk1977 tags: trunk)
06:16
Documentation spellcheck and cleanup. No changes to code. (CVS 5258) (check-in: 2904d26ba4 user: mihailim tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/malloc.c.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** Memory allocation functions used throughout sqlite.
**
** $Id: malloc.c,v 1.22 2008/06/19 18:17:50 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** This routine runs when the memory allocator sees that the







|







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** Memory allocation functions used throughout sqlite.
**
** $Id: malloc.c,v 1.23 2008/06/21 08:12:15 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** This routine runs when the memory allocator sees that the
401
402
403
404
405
406
407




408
409
410
411
412
413
414
415
416
417
418





419
420
421
422
423
424
425
426
427






428
429
430
431
432
433
434
  return p;    
}
void sqlite3PageFree(void *p){
  if( p ){
    if( sqlite3Config.pPage==0
           || p<sqlite3Config.pPage
           || p>=(void*)mem0.aPageFree ){




      if( sqlite3Config.bMemstat ){
        int iSize = sqlite3MallocSize(p);
        sqlite3_mutex_enter(mem0.mutex);
        sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
        sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
        sqlite3Config.m.xFree(p);
        sqlite3_mutex_leave(mem0.mutex);
      }else{
        sqlite3Config.m.xFree(p);
      }
    }else{





      int i;
      i = p - sqlite3Config.pPage;
      i /= sqlite3Config.szPage;
      assert( i>=0 && i<sqlite3Config.nPage );
      sqlite3_mutex_enter(mem0.mutex);
      assert( mem0.nPageFree<sqlite3Config.nPage );
      mem0.aPageFree[mem0.nPageFree++] = i;
      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
      sqlite3_mutex_leave(mem0.mutex);






    }
  }
}

/*
** Return the size of a memory allocation previously obtained from
** sqlite3Malloc() or sqlite3_malloc().







>
>
>
>











>
>
>
>
>









>
>
>
>
>
>







401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
  return p;    
}
void sqlite3PageFree(void *p){
  if( p ){
    if( sqlite3Config.pPage==0
           || p<sqlite3Config.pPage
           || p>=(void*)mem0.aPageFree ){
      /* In this case, the page allocation was obtained from a regular 
      ** call to sqlite3_mem_methods.xMalloc() (a page-cache-memory 
      ** "overflow"). Free the block with sqlite3_mem_methods.xFree().
      */
      if( sqlite3Config.bMemstat ){
        int iSize = sqlite3MallocSize(p);
        sqlite3_mutex_enter(mem0.mutex);
        sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
        sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
        sqlite3Config.m.xFree(p);
        sqlite3_mutex_leave(mem0.mutex);
      }else{
        sqlite3Config.m.xFree(p);
      }
    }else{
      /* The page allocation was allocated from the sqlite3Config.pPage
      ** buffer. In this case all that is add the index of the page in
      ** the sqlite3Config.pPage array to the set of free indexes stored
      ** in the mem0.aPageFree[] array.
      */
      int i;
      i = p - sqlite3Config.pPage;
      i /= sqlite3Config.szPage;
      assert( i>=0 && i<sqlite3Config.nPage );
      sqlite3_mutex_enter(mem0.mutex);
      assert( mem0.nPageFree<sqlite3Config.nPage );
      mem0.aPageFree[mem0.nPageFree++] = i;
      sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
      sqlite3_mutex_leave(mem0.mutex);
#ifndef NDEBUG
      /* Assert that a duplicate was not just inserted into aPageFree[]. */
      for(i=0; i<mem0.nPageFree-1; i++){
        assert( mem0.aPageFree[i]!=mem0.aPageFree[mem0.nPageFree-1] );
      }
#endif
    }
  }
}

/*
** Return the size of a memory allocation previously obtained from
** sqlite3Malloc() or sqlite3_malloc().
Changes to test/tableapi.test.
8
9
10
11
12
13
14
15
16
17
18











19
20
21
22
23
24
25
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the sqlite_exec_printf() and
# sqlite_get_table_printf() APIs.
#
# $Id: tableapi.test,v 1.16 2008/03/17 15:09:48 drh Exp $

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












do_test tableapi-1.0 {
  set ::dbx [sqlite3_open test.db]
  catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}}
  sqlite3_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz
} {0 {}}
do_test tableapi-1.1 {







|



>
>
>
>
>
>
>
>
>
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.  The
# focus of this file is testing the sqlite_exec_printf() and
# sqlite_get_table_printf() APIs.
#
# $Id: tableapi.test,v 1.17 2008/06/21 08:12:15 danielk1977 Exp $

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

ifcapable memdebug {
  source $testdir/malloc_common.tcl
}

db close
sqlite3_shutdown
sqlite3_config_pagecache 4096 24
sqlite3_config_scratch 25000 1
sqlite3_initialize
sqlite3 db test.db

do_test tableapi-1.0 {
  set ::dbx [sqlite3_open test.db]
  catch {sqlite_exec_printf $::dbx {DROP TABLE xyz} {}}
  sqlite3_exec_printf $::dbx {CREATE TABLE %s(a int, b text)} xyz
} {0 {}}
do_test tableapi-1.1 {
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
ifcapable schema_pragmas {
  do_test tableapi-6.1 {
    sqlite3_get_table_printf $::dbx {PRAGMA user_version} {}
  } {0 1 1 user_version 0}
}

ifcapable memdebug {
  source $testdir/malloc_common.tcl
  do_malloc_test tableapi-7 -sqlprep {
    DROP TABLE IF EXISTS t1;
    CREATE TABLE t1(a,b);
    INSERT INTO t1 VALUES(1,2);
    INSERT INTO t1 VALUES(3,4);
    INSERT INTO t1 SELECT a+4, b+4 FROM t1;
    INSERT INTO t1 SELECT a+8, b+8 FROM t1;







<







231
232
233
234
235
236
237

238
239
240
241
242
243
244
ifcapable schema_pragmas {
  do_test tableapi-6.1 {
    sqlite3_get_table_printf $::dbx {PRAGMA user_version} {}
  } {0 1 1 user_version 0}
}

ifcapable memdebug {

  do_malloc_test tableapi-7 -sqlprep {
    DROP TABLE IF EXISTS t1;
    CREATE TABLE t1(a,b);
    INSERT INTO t1 VALUES(1,2);
    INSERT INTO t1 VALUES(3,4);
    INSERT INTO t1 SELECT a+4, b+4 FROM t1;
    INSERT INTO t1 SELECT a+8, b+8 FROM t1;