/ Hex Artifact Content
Login

Artifact 99b1999dbfa7cc04aaeac9d695a2445d4e7c7458:


0000: 23 20 32 30 30 39 20 4a 75 6e 65 20 33 0a 23 0a  # 2009 June 3.#.
0010: 23 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  # The author dis
0020: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
0030: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
0040: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
0050: 6f 66 0a 23 20 61 20 6c 65 67 61 6c 20 6e 6f 74  of.# a legal not
0060: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
0070: 6c 65 73 73 69 6e 67 3a 0a 23 0a 23 20 20 20 20  lessing:.#.#    
0080: 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
0090: 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 23 20  and not evil..# 
00a0: 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
00b0: 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
00c0: 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
00d0: 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 23 20 20  give others..#  
00e0: 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
00f0: 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
0100: 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
0110: 6f 75 20 67 69 76 65 2e 0a 23 0a 23 2a 2a 2a 2a  ou give..#.#****
0120: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0130: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0140: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0150: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0160: 2a 2a 2a 0a 23 0a 23 20 24 49 64 3a 20 63 6f 72  ***.#.# $Id: cor
0170: 72 75 70 74 44 2e 74 65 73 74 2c 76 20 31 2e 32  ruptD.test,v 1.2
0180: 20 32 30 30 39 2f 30 36 2f 30 35 20 31 37 3a 30   2009/06/05 17:0
0190: 39 3a 31 32 20 64 72 68 20 45 78 70 20 24 0a 0a  9:12 drh Exp $..
01a0: 73 65 74 20 74 65 73 74 64 69 72 20 5b 66 69 6c  set testdir [fil
01b0: 65 20 64 69 72 6e 61 6d 65 20 24 61 72 67 76 30  e dirname $argv0
01c0: 5d 0a 73 6f 75 72 63 65 20 24 74 65 73 74 64 69  ].source $testdi
01d0: 72 2f 74 65 73 74 65 72 2e 74 63 6c 0a 0a 23 20  r/tester.tcl..# 
01e0: 44 6f 20 6e 6f 74 20 75 73 65 20 61 20 63 6f 64  Do not use a cod
01f0: 65 63 20 66 6f 72 20 74 65 73 74 73 20 69 6e 20  ec for tests in 
0200: 74 68 69 73 20 66 69 6c 65 2c 20 61 73 20 74 68  this file, as th
0210: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
0220: 69 73 0a 23 20 6d 61 6e 69 70 75 6c 61 74 65 64  is.# manipulated
0230: 20 64 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20   directly using 
0240: 74 63 6c 20 73 63 72 69 70 74 73 20 28 75 73 69  tcl scripts (usi
0250: 6e 67 20 74 68 65 20 5b 68 65 78 69 6f 5f 77 72  ng the [hexio_wr
0260: 69 74 65 5d 20 63 6f 6d 6d 61 6e 64 29 2e 0a 23  ite] command)..#
0270: 0a 64 6f 5f 6e 6f 74 5f 75 73 65 5f 63 6f 64 65  .do_not_use_code
0280: 63 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  c..#------------
0290: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02b0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
02c0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
02d0: 20 4f 56 45 52 56 49 45 57 0a 23 0a 23 20 20 20   OVERVIEW.#.#   
02e0: 54 68 69 73 20 74 65 73 74 20 66 69 6c 65 20 61  This test file a
02f0: 74 74 65 6d 70 74 73 20 74 6f 20 76 65 72 69 66  ttempts to verif
0300: 79 20 74 68 61 74 20 53 51 4c 69 74 65 20 64 6f  y that SQLite do
0310: 65 73 20 6e 6f 74 20 72 65 61 64 20 70 61 73 74  es not read past
0320: 20 74 68 65 20 0a 23 20 20 20 65 6e 64 20 6f 66   the .#   end of
0330: 20 61 6e 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 62   any in-memory b
0340: 75 66 66 65 72 73 20 61 73 20 61 20 72 65 73 75  uffers as a resu
0350: 6c 74 20 6f 66 20 63 6f 72 72 75 70 74 65 64 20  lt of corrupted 
0360: 64 61 74 61 62 61 73 65 20 70 61 67 65 20 0a 23  database page .#
0370: 20 20 20 69 6d 61 67 65 73 2e 20 55 73 75 61 6c     images. Usual
0380: 6c 79 20 74 68 69 73 20 68 61 70 70 65 6e 73 20  ly this happens 
0390: 62 65 63 61 75 73 65 20 61 20 66 69 65 6c 64 20  because a field 
03a0: 77 69 74 68 69 6e 20 61 20 64 61 74 61 62 61 73  within a databas
03b0: 65 20 70 61 67 65 0a 23 20 20 20 74 68 61 74 20  e page.#   that 
03c0: 63 6f 6e 74 61 69 6e 73 20 61 6e 20 6f 66 66 73  contains an offs
03d0: 65 74 20 74 6f 20 73 6f 6d 65 20 6f 74 68 65 72  et to some other
03e0: 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 69   structure withi
03f0: 6e 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 0a  n the same page.
0400: 23 20 20 20 69 73 20 73 65 74 20 74 6f 20 74 6f  #   is set to to
0410: 6f 20 6c 61 72 67 65 20 61 20 76 61 6c 75 65 2e  o large a value.
0420: 20 41 20 64 61 74 61 62 61 73 65 20 70 61 67 65   A database page
0430: 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 6f   contains the fo
0440: 6c 6c 6f 77 69 6e 67 0a 23 20 20 20 73 75 63 68  llowing.#   such
0450: 20 66 69 65 6c 64 73 3a 0a 23 0a 23 20 20 20 20   fields:.#.#    
0460: 20 31 2e 20 54 68 65 20 70 61 67 65 20 68 65 61   1. The page hea
0470: 64 65 72 20 66 69 65 6c 64 20 74 68 61 74 20 63  der field that c
0480: 6f 6e 74 61 69 6e 73 20 74 68 65 20 6f 66 66 73  ontains the offs
0490: 65 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  et to the first 
04a0: 0a 23 20 20 20 20 20 20 20 20 66 72 65 65 20 62  .#        free b
04b0: 6c 6f 63 6b 20 6f 66 20 73 70 61 63 65 2e 0a 23  lock of space..#
04c0: 0a 23 20 20 20 20 20 32 2e 20 54 68 65 20 66 69  .#     2. The fi
04d0: 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f 66  rst two bytes of
04e0: 20 61 6c 6c 20 62 75 74 20 74 68 65 20 6c 61 73   all but the las
04f0: 74 20 66 72 65 65 20 62 6c 6f 63 6b 20 6f 6e 20  t free block on 
0500: 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 0a 23  the free-block.#
0510: 20 20 20 20 20 20 20 20 6c 69 73 74 20 28 74 68          list (th
0520: 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20  e offset to the 
0530: 6e 65 78 74 20 66 72 65 65 20 62 6c 6f 63 6b 29  next free block)
0540: 2e 0a 23 0a 23 20 20 20 20 20 33 2e 20 54 68 65  ..#.#     3. The
0550: 20 70 61 67 65 20 68 65 61 64 65 72 20 66 69 65   page header fie
0560: 6c 64 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ld containing th
0570: 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  e number of cell
0580: 73 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 23 20  s on the page.# 
0590: 20 20 20 20 20 20 20 28 69 6d 70 6c 69 63 69 74         (implicit
05a0: 6c 79 20 64 65 66 69 6e 65 73 20 74 68 65 20 6f  ly defines the o
05b0: 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69 6e  ffset to the fin
05c0: 61 6c 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74 68  al element in th
05d0: 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 0a 23 20  e cell offset.# 
05e0: 20 20 20 20 20 20 20 61 72 72 61 79 2c 20 77 68         array, wh
05f0: 69 63 68 20 63 6f 75 6c 64 20 70 6f 74 65 6e 74  ich could potent
0600: 69 61 6c 6c 79 20 62 65 20 6f 66 66 20 74 68 65  ially be off the
0610: 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65   end of the page
0620: 29 2e 0a 23 0a 23 20 20 20 20 20 34 2e 20 54 68  )..#.#     4. Th
0630: 65 20 70 61 67 65 20 68 65 61 64 65 72 20 66 69  e page header fi
0640: 65 6c 64 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  eld containing t
0650: 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65  he offset to the
0660: 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 63 65   start of the ce
0670: 6c 6c 0a 23 20 20 20 20 20 20 20 20 63 6f 6e 74  ll.#        cont
0680: 65 6e 74 20 61 72 65 61 2e 0a 23 0a 23 20 20 20  ent area..#.#   
0690: 20 20 35 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74    5. The content
06a0: 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 6f 66  s of the cell of
06b0: 66 73 65 74 20 61 72 72 61 79 2e 0a 23 0a 23 20  fset array..#.# 
06c0: 20 20 20 20 36 2e 20 54 68 65 20 66 69 72 73 74      6. The first
06d0: 20 66 65 77 20 62 79 74 65 73 20 6f 66 20 65 61   few bytes of ea
06e0: 63 68 20 63 65 6c 6c 20 64 65 74 65 72 6d 69 6e  ch cell determin
06f0: 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  e the size of th
0700: 65 20 63 65 6c 6c 0a 23 20 20 20 20 20 20 20 20  e cell.#        
0710: 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68  stored within th
0720: 65 20 70 61 67 65 2c 20 61 6e 64 20 68 65 6e 63  e page, and henc
0730: 65 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20  e the offset to 
0740: 74 68 65 20 66 69 6e 61 6c 20 62 79 74 65 20 6f  the final byte o
0750: 66 0a 23 20 20 20 20 20 20 20 20 74 68 65 20 63  f.#        the c
0760: 65 6c 6c 2e 0a 23 0a 23 20 20 20 49 66 20 61 6e  ell..#.#   If an
0770: 79 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 66  y of the above f
0780: 69 65 6c 64 73 20 61 72 65 20 73 65 74 20 74 6f  ields are set to
0790: 20 74 6f 6f 20 6c 61 72 67 65 20 61 20 76 61 6c   too large a val
07a0: 75 65 2c 20 74 68 65 6e 20 61 20 62 75 66 66 65  ue, then a buffe
07b0: 72 0a 23 20 20 20 6f 76 65 72 72 65 61 64 20 6d  r.#   overread m
07c0: 61 79 20 6f 63 63 75 72 2e 20 54 68 69 73 20 74  ay occur. This t
07d0: 65 73 74 20 73 63 72 69 70 74 20 63 72 65 61 74  est script creat
07e0: 65 73 20 61 6e 64 20 6f 70 65 72 61 74 65 73 20  es and operates 
07f0: 6f 6e 20 76 61 72 69 6f 75 73 0a 23 20 20 20 73  on various.#   s
0800: 74 72 61 74 65 67 69 63 61 6c 6c 79 20 63 6f 72  trategically cor
0810: 72 75 70 74 65 64 20 64 61 74 61 62 61 73 65 20  rupted database 
0820: 66 69 6c 65 73 20 74 6f 20 61 74 74 65 6d 70 74  files to attempt
0830: 20 74 6f 20 70 72 6f 76 6f 6b 65 20 73 75 63 68   to provoke such
0840: 20 62 75 66 66 65 72 0a 23 20 20 20 6f 76 65 72   buffer.#   over
0850: 72 65 61 64 73 2e 0a 23 0a 23 20 20 20 56 65 72  reads..#.#   Ver
0860: 79 20 6f 66 74 65 6e 2c 20 61 20 62 75 66 66 65  y often, a buffe
0870: 72 20 6f 76 65 72 72 65 61 64 20 70 61 73 73 65  r overread passe
0880: 73 20 75 6e 6e 6f 74 69 63 65 64 2c 20 70 61 72  s unnoticed, par
0890: 74 69 63 75 6c 61 72 6c 79 20 69 6e 20 77 6f 72  ticularly in wor
08a0: 6b 73 74 61 74 69 6f 6e 0a 23 20 20 20 65 6e 76  kstation.#   env
08b0: 69 72 6f 6e 6d 65 6e 74 73 2e 20 46 6f 72 20 74  ironments. For t
08c0: 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 69 73  his reason, this
08d0: 20 74 65 73 74 20 73 63 72 69 70 74 20 73 68 6f   test script sho
08e0: 75 6c 64 20 62 65 20 72 75 6e 20 75 73 69 6e 67  uld be run using
08f0: 20 76 61 6c 67 72 69 6e 64 0a 23 20 20 20 28 6f   valgrind.#   (o
0900: 72 20 73 69 6d 69 6c 61 72 29 20 69 6e 20 6f 72  r similar) in or
0910: 64 65 72 20 74 6f 20 76 65 72 69 66 79 20 74 68  der to verify th
0920: 61 74 20 6e 6f 20 6f 76 65 72 72 65 61 64 73 20  at no overreads 
0930: 6f 63 63 75 72 2e 0a 23 0a 23 20 54 45 53 54 20  occur..#.# TEST 
0940: 50 4c 41 4e 0a 23 20 0a 23 20 20 20 54 65 73 74  PLAN.# .#   Test
0950: 20 63 61 73 65 73 20 63 6f 72 72 75 70 74 44 2d   cases corruptD-
0960: 31 2e 2a 20 61 72 65 20 77 68 69 74 65 2d 62 6f  1.* are white-bo
0970: 78 20 74 65 73 74 73 2e 20 54 68 65 79 20 61 74  x tests. They at
0980: 74 65 6d 70 74 20 74 6f 20 63 6f 72 72 75 70 74  tempt to corrupt
0990: 0a 23 20 20 20 6f 6e 65 20 6f 66 20 74 68 65 20  .#   one of the 
09a0: 61 62 6f 76 65 20 66 69 65 6c 64 73 2c 20 74 68  above fields, th
09b0: 65 6e 20 65 78 65 72 63 69 73 65 20 65 61 63 68  en exercise each
09c0: 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 6f 64   part of the cod
09d0: 65 20 69 6e 20 62 74 72 65 65 2e 63 0a 23 20 20  e in btree.c.#  
09e0: 20 74 68 61 74 20 75 73 65 73 20 73 61 69 64 20   that uses said 
09f0: 66 69 65 6c 64 2e 0a 23 20 20 20 0a 23 20 20 20  field..#   .#   
0a00: 4f 66 66 73 65 74 20 76 61 72 69 61 62 6c 65 73  Offset variables
0a10: 20 31 2c 20 32 2c 20 33 20 61 6e 64 20 34 20 61   1, 2, 3 and 4 a
0a20: 72 65 20 61 6c 6c 20 63 68 65 63 6b 65 64 20 74  re all checked t
0a30: 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79  o make sure they
0a40: 0a 23 20 20 20 77 69 6c 6c 20 6e 6f 74 20 72 65  .#   will not re
0a50: 73 75 6c 74 20 69 6e 20 62 75 66 66 65 72 20 6f  sult in buffer o
0a60: 76 65 72 72 75 6e 73 20 61 73 20 70 61 72 74 20  verruns as part 
0a70: 6f 66 20 70 61 67 65 20 69 6e 69 74 69 61 6c 69  of page initiali
0a80: 7a 61 74 69 6f 6e 20 69 6e 0a 23 20 20 20 73 71  zation in.#   sq
0a90: 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74 50 61  lite3BtreeInitPa
0aa0: 67 65 28 29 2e 20 4f 66 66 73 65 74 73 20 35 20  ge(). Offsets 5 
0ab0: 61 6e 64 20 36 20 63 61 6e 6e 6f 74 20 62 65 20  and 6 cannot be 
0ac0: 74 65 73 74 65 64 20 61 73 20 70 61 72 74 20 6f  tested as part o
0ad0: 66 0a 23 20 20 20 70 61 67 65 20 69 6e 69 74 69  f.#   page initi
0ae0: 61 6c 69 7a 61 74 69 6f 6e 2c 20 61 73 20 74 72  alization, as tr
0af0: 79 69 6e 67 20 74 6f 20 64 6f 20 73 6f 20 63 61  ying to do so ca
0b00: 75 73 65 73 20 61 20 70 65 72 66 6f 72 6d 61 6e  uses a performan
0b10: 63 65 20 68 69 74 2e 0a 23 0a 0a 64 6f 5f 74 65  ce hit..#..do_te
0b20: 73 74 20 63 6f 72 72 75 70 74 44 2d 31 2e 30 20  st corruptD-1.0 
0b30: 7b 0a 20 20 65 78 65 63 73 71 6c 20 7b 20 0a 20  {.  execsql { . 
0b40: 20 20 20 50 52 41 47 4d 41 20 61 75 74 6f 5f 76     PRAGMA auto_v
0b50: 61 63 75 75 6d 20 3d 20 30 3b 0a 20 20 20 20 50  acuum = 0;.    P
0b60: 52 41 47 4d 41 20 70 61 67 65 5f 73 69 7a 65 20  RAGMA page_size 
0b70: 3d 20 31 30 32 34 3b 0a 20 20 20 20 43 52 45 41  = 1024;.    CREA
0b80: 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62  TE TABLE t1(a, b
0b90: 29 3b 0a 20 20 20 20 43 52 45 41 54 45 20 49 4e  );.    CREATE IN
0ba0: 44 45 58 20 69 31 20 4f 4e 20 74 31 28 61 2c 20  DEX i1 ON t1(a, 
0bb0: 62 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 20 7b 73  b);.  }.  for {s
0bc0: 65 74 20 69 69 20 31 7d 20 7b 24 69 69 20 3c 20  et ii 1} {$ii < 
0bd0: 35 30 7d 20 7b 69 6e 63 72 20 69 69 7d 20 7b 0a  50} {incr ii} {.
0be0: 20 20 20 20 65 78 65 63 73 71 6c 20 7b 20 49 4e      execsql { IN
0bf0: 53 45 52 54 20 49 4e 54 4f 20 74 31 20 56 41 4c  SERT INTO t1 VAL
0c00: 55 45 53 28 24 69 69 2c 20 24 69 69 20 2a 20 24  UES($ii, $ii * $
0c10: 69 69 29 20 7d 0a 20 20 7d 0a 20 20 65 78 65 63  ii) }.  }.  exec
0c20: 73 71 6c 20 7b 0a 20 20 20 20 44 45 4c 45 54 45  sql {.    DELETE
0c30: 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
0c40: 20 3d 20 31 30 3b 0a 20 20 20 20 44 45 4c 45 54   = 10;.    DELET
0c50: 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  E FROM t1 WHERE 
0c60: 61 20 3d 20 32 30 3b 0a 20 20 20 20 44 45 4c 45  a = 20;.    DELE
0c70: 54 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45  TE FROM t1 WHERE
0c80: 20 61 20 3d 20 33 30 3b 0a 20 20 20 20 44 45 4c   a = 30;.    DEL
0c90: 45 54 45 20 46 52 4f 4d 20 74 31 20 57 48 45 52  ETE FROM t1 WHER
0ca0: 45 20 61 20 3d 20 34 30 3b 0a 20 20 7d 0a 20 20  E a = 40;.  }.  
0cb0: 66 6f 72 63 65 63 6f 70 79 20 74 65 73 74 2e 64  forcecopy test.d
0cc0: 62 20 74 65 73 74 2e 62 75 0a 7d 20 7b 7d 0a 0a  b test.bu.} {}..
0cd0: 70 72 6f 63 20 69 6e 63 72 5f 63 68 61 6e 67 65  proc incr_change
0ce0: 5f 63 6f 75 6e 74 65 72 20 7b 7d 20 7b 0a 20 20  _counter {} {.  
0cf0: 68 65 78 69 6f 5f 77 72 69 74 65 20 74 65 73 74  hexio_write test
0d00: 2e 64 62 20 32 34 20 5b 0a 20 20 20 20 68 65 78  .db 24 [.    hex
0d10: 69 6f 5f 72 65 6e 64 65 72 5f 69 6e 74 33 32 20  io_render_int32 
0d20: 5b 65 78 70 72 20 5b 68 65 78 69 6f 5f 67 65 74  [expr [hexio_get
0d30: 5f 69 6e 74 20 5b 68 65 78 69 6f 5f 72 65 61 64  _int [hexio_read
0d40: 20 74 65 73 74 2e 64 62 20 32 34 20 34 5d 5d 20   test.db 24 4]] 
0d50: 2b 20 31 5d 0a 20 20 5d 0a 7d 0a 0a 70 72 6f 63  + 1].  ].}..proc
0d60: 20 72 65 73 74 6f 72 65 5f 66 69 6c 65 20 7b 7d   restore_file {}
0d70: 20 7b 0a 20 20 64 62 20 63 6c 6f 73 65 0a 20 20   {.  db close.  
0d80: 66 6f 72 63 65 63 6f 70 79 20 74 65 73 74 2e 62  forcecopy test.b
0d90: 75 20 74 65 73 74 2e 64 62 0a 20 20 73 71 6c 69  u test.db.  sqli
0da0: 74 65 33 20 64 62 20 74 65 73 74 2e 64 62 0a 7d  te3 db test.db.}
0db0: 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ..#-------------
0dc0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0dd0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0de0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0df0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54  ------------.# T
0e00: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73  he following tes
0e10: 74 73 2c 20 63 6f 72 72 75 70 74 44 2d 31 2e 31  ts, corruptD-1.1
0e20: 2e 2a 2c 20 66 6f 63 75 73 20 6f 6e 20 74 68 65  .*, focus on the
0e30: 20 70 61 67 65 20 68 65 61 64 65 72 20 66 69 65   page header fie
0e40: 6c 64 0a 23 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ld.# containing 
0e50: 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68  the offset of th
0e60: 65 20 66 69 72 73 74 20 66 72 65 65 20 62 6c 6f  e first free blo
0e70: 63 6b 20 69 6e 20 61 20 70 61 67 65 2e 20 0a 23  ck in a page. .#
0e80: 0a 64 6f 5f 74 65 73 74 20 63 6f 72 72 75 70 74  .do_test corrupt
0e90: 44 2d 31 2e 31 2e 31 20 7b 0a 20 20 69 6e 63 72  D-1.1.1 {.  incr
0ea0: 5f 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 0a  _change_counter.
0eb0: 20 20 68 65 78 69 6f 5f 77 72 69 74 65 20 74 65    hexio_write te
0ec0: 73 74 2e 64 62 20 5b 65 78 70 72 20 31 30 32 34  st.db [expr 1024
0ed0: 2b 31 5d 20 46 46 46 46 0a 20 20 63 61 74 63 68  +1] FFFF.  catch
0ee0: 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20 46  sql { SELECT * F
0ef0: 52 4f 4d 20 74 31 20 7d 0a 7d 20 7b 31 20 7b 64  ROM t1 }.} {1 {d
0f00: 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61  atabase disk ima
0f10: 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 7d  ge is malformed}
0f20: 7d 0a 64 6f 5f 74 65 73 74 20 63 6f 72 72 75 70  }.do_test corrup
0f30: 74 44 2d 31 2e 31 2e 32 20 7b 0a 20 20 69 6e 63  tD-1.1.2 {.  inc
0f40: 72 5f 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72  r_change_counter
0f50: 0a 20 20 68 65 78 69 6f 5f 77 72 69 74 65 20 74  .  hexio_write t
0f60: 65 73 74 2e 64 62 20 5b 65 78 70 72 20 31 30 32  est.db [expr 102
0f70: 34 2b 31 5d 20 5b 68 65 78 69 6f 5f 72 65 6e 64  4+1] [hexio_rend
0f80: 65 72 5f 69 6e 74 33 32 20 31 30 32 31 5d 0a 20  er_int32 1021]. 
0f90: 20 63 61 74 63 68 73 71 6c 20 7b 20 53 45 4c 45   catchsql { SELE
0fa0: 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 7d 0a 7d  CT * FROM t1 }.}
0fb0: 20 7b 31 20 7b 64 61 74 61 62 61 73 65 20 64 69   {1 {database di
0fc0: 73 6b 20 69 6d 61 67 65 20 69 73 20 6d 61 6c 66  sk image is malf
0fd0: 6f 72 6d 65 64 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d  ormed}}..#------
0fe0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0ff0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1000: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1010: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1020: 2d 2d 2d 0a 23 20 54 68 65 20 66 6f 6c 6c 6f 77  ---.# The follow
1030: 69 6e 67 20 74 65 73 74 73 2c 20 63 6f 72 72 75  ing tests, corru
1040: 70 74 44 2d 31 2e 32 2e 2a 2c 20 66 6f 63 75 73  ptD-1.2.*, focus
1050: 20 6f 6e 20 74 68 65 20 6f 66 66 73 65 74 73 20   on the offsets 
1060: 63 6f 6e 74 61 69 6e 65 64 0a 23 20 69 6e 20 74  contained.# in t
1070: 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65 20  he first 2 byte 
1080: 6f 66 20 65 61 63 68 20 66 72 65 65 2d 62 6c 6f  of each free-blo
1090: 63 6b 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  ck on the free-l
10a0: 69 73 74 2e 0a 23 0a 64 6f 5f 74 65 73 74 20 63  ist..#.do_test c
10b0: 6f 72 72 75 70 74 44 2d 31 2e 32 2e 31 20 7b 0a  orruptD-1.2.1 {.
10c0: 20 20 72 65 73 74 6f 72 65 5f 66 69 6c 65 0a 7d    restore_file.}
10d0: 20 7b 7d 0a 64 6f 5f 74 65 73 74 20 63 6f 72 72   {}.do_test corr
10e0: 75 70 74 44 2d 31 2e 32 2e 32 20 7b 0a 7d 20 7b  uptD-1.2.2 {.} {
10f0: 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }..#------------
1100: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1110: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1120: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1130: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20  -------------.# 
1140: 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 65  The following te
1150: 73 74 73 2c 20 63 6f 72 72 75 70 74 44 2d 31 2e  sts, corruptD-1.
1160: 34 2e 2a 2c 20 2e 2e 2e 0a 23 0a 0a 0a 23 2d 2d  4.*, ....#...#--
1170: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1180: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1190: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11b0: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 65 20 66 6f  -------.# The fo
11c0: 6c 6c 6f 77 69 6e 67 20 74 65 73 74 73 2c 20 63  llowing tests, c
11d0: 6f 72 72 75 70 74 44 2d 31 2e 35 2e 2a 2c 20 66  orruptD-1.5.*, f
11e0: 6f 63 75 73 20 6f 6e 20 74 68 65 20 6f 66 66 73  ocus on the offs
11f0: 65 74 73 20 63 6f 6e 74 61 69 6e 65 64 0a 23 20  ets contained.# 
1200: 69 6e 20 74 68 65 20 63 65 6c 6c 20 6f 66 66 73  in the cell offs
1210: 65 74 20 61 72 72 61 79 2e 0a 23 20 0a 23 20 20  et array..# .#  
1220: 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67 65 0a   defragmentPage.
1230: 23 0a 0a 66 69 6e 69 73 68 5f 74 65 73 74 0a     #..finish_test.