/ Hex Artifact Content
Login

Artifact 3ae6e2dc6e2226c6935a8a40d4b5ee3eba75f8c0:


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 2d  r/tester.tcl..#-
01e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
01f0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0200: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0210: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0220: 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 4f 56 45 52  ---------.# OVER
0230: 56 49 45 57 0a 23 0a 23 20 20 20 54 68 69 73 20  VIEW.#.#   This 
0240: 74 65 73 74 20 66 69 6c 65 20 61 74 74 65 6d 70  test file attemp
0250: 74 73 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  ts to verify tha
0260: 74 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f  t SQLite does no
0270: 74 20 72 65 61 64 20 70 61 73 74 20 74 68 65 20  t read past the 
0280: 0a 23 20 20 20 65 6e 64 20 6f 66 20 61 6e 79 20  .#   end of any 
0290: 69 6e 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  in-memory buffer
02a0: 73 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  s as a result of
02b0: 20 63 6f 72 72 75 70 74 65 64 20 64 61 74 61 62   corrupted datab
02c0: 61 73 65 20 70 61 67 65 20 0a 23 20 20 20 69 6d  ase page .#   im
02d0: 61 67 65 73 2e 20 55 73 75 61 6c 6c 79 20 74 68  ages. Usually th
02e0: 69 73 20 68 61 70 70 65 6e 73 20 62 65 63 61 75  is happens becau
02f0: 73 65 20 61 20 66 69 65 6c 64 20 77 69 74 68 69  se a field withi
0300: 6e 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  n a database pag
0310: 65 0a 23 20 20 20 74 68 61 74 20 63 6f 6e 74 61  e.#   that conta
0320: 69 6e 73 20 61 6e 20 6f 66 66 73 65 74 20 74 6f  ins an offset to
0330: 20 73 6f 6d 65 20 6f 74 68 65 72 20 73 74 72 75   some other stru
0340: 63 74 75 72 65 20 77 69 74 68 69 6e 20 74 68 65  cture within the
0350: 20 73 61 6d 65 20 70 61 67 65 0a 23 20 20 20 69   same page.#   i
0360: 73 20 73 65 74 20 74 6f 20 74 6f 6f 20 6c 61 72  s set to too lar
0370: 67 65 20 61 20 76 61 6c 75 65 2e 20 41 20 64 61  ge a value. A da
0380: 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 6e 74  tabase page cont
0390: 61 69 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  ains the followi
03a0: 6e 67 0a 23 20 20 20 73 75 63 68 20 66 69 65 6c  ng.#   such fiel
03b0: 64 73 3a 0a 23 0a 23 20 20 20 20 20 31 2e 20 54  ds:.#.#     1. T
03c0: 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 66  he page header f
03d0: 69 65 6c 64 20 74 68 61 74 20 63 6f 6e 74 61 69  ield that contai
03e0: 6e 73 20 74 68 65 20 6f 66 66 73 65 74 20 74 6f  ns the offset to
03f0: 20 74 68 65 20 66 69 72 73 74 20 0a 23 20 20 20   the first .#   
0400: 20 20 20 20 20 66 72 65 65 20 62 6c 6f 63 6b 20       free block 
0410: 6f 66 20 73 70 61 63 65 2e 0a 23 0a 23 20 20 20  of space..#.#   
0420: 20 20 32 2e 20 54 68 65 20 66 69 72 73 74 20 74    2. The first t
0430: 77 6f 20 62 79 74 65 73 20 6f 66 20 61 6c 6c 20  wo bytes of all 
0440: 62 75 74 20 74 68 65 20 6c 61 73 74 20 66 72 65  but the last fre
0450: 65 20 62 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66  e block on the f
0460: 72 65 65 2d 62 6c 6f 63 6b 0a 23 20 20 20 20 20  ree-block.#     
0470: 20 20 20 6c 69 73 74 20 28 74 68 65 20 6f 66 66     list (the off
0480: 73 65 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20  set to the next 
0490: 66 72 65 65 20 62 6c 6f 63 6b 29 2e 0a 23 0a 23  free block)..#.#
04a0: 20 20 20 20 20 33 2e 20 54 68 65 20 70 61 67 65       3. The page
04b0: 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 63 6f   header field co
04c0: 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 75 6d  ntaining the num
04d0: 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20  ber of cells on 
04e0: 74 68 65 20 70 61 67 65 0a 23 20 20 20 20 20 20  the page.#      
04f0: 20 20 28 69 6d 70 6c 69 63 69 74 6c 79 20 64 65    (implicitly de
0500: 66 69 6e 65 73 20 74 68 65 20 6f 66 66 73 65 74  fines the offset
0510: 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 65 6c   to the final el
0520: 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c  ement in the cel
0530: 6c 20 6f 66 66 73 65 74 0a 23 20 20 20 20 20 20  l offset.#      
0540: 20 20 61 72 72 61 79 2c 20 77 68 69 63 68 20 63    array, which c
0550: 6f 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c 6c 79  ould potentially
0560: 20 62 65 20 6f 66 66 20 74 68 65 20 65 6e 64 20   be off the end 
0570: 6f 66 20 74 68 65 20 70 61 67 65 29 2e 0a 23 0a  of the page)..#.
0580: 23 20 20 20 20 20 34 2e 20 54 68 65 20 70 61 67  #     4. The pag
0590: 65 20 68 65 61 64 65 72 20 66 69 65 6c 64 20 63  e header field c
05a0: 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f 66  ontaining the of
05b0: 66 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72  fset to the star
05c0: 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 0a 23 20  t of the cell.# 
05d0: 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 20 61         content a
05e0: 72 65 61 2e 0a 23 0a 23 20 20 20 20 20 35 2e 20  rea..#.#     5. 
05f0: 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
0600: 74 68 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20  the cell offset 
0610: 61 72 72 61 79 2e 0a 23 0a 23 20 20 20 20 20 36  array..#.#     6
0620: 2e 20 54 68 65 20 66 69 72 73 74 20 66 65 77 20  . The first few 
0630: 62 79 74 65 73 20 6f 66 20 65 61 63 68 20 63 65  bytes of each ce
0640: 6c 6c 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  ll determine the
0650: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 63 65 6c   size of the cel
0660: 6c 0a 23 20 20 20 20 20 20 20 20 73 74 6f 72 65  l.#        store
0670: 64 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67  d within the pag
0680: 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65  e, and hence the
0690: 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66   offset to the f
06a0: 69 6e 61 6c 20 62 79 74 65 20 6f 66 0a 23 20 20  inal byte of.#  
06b0: 20 20 20 20 20 20 74 68 65 20 63 65 6c 6c 2e 0a        the cell..
06c0: 23 0a 23 20 20 20 49 66 20 61 6e 79 20 6f 66 20  #.#   If any of 
06d0: 74 68 65 20 61 62 6f 76 65 20 66 69 65 6c 64 73  the above fields
06e0: 20 61 72 65 20 73 65 74 20 74 6f 20 74 6f 6f 20   are set to too 
06f0: 6c 61 72 67 65 20 61 20 76 61 6c 75 65 2c 20 74  large a value, t
0700: 68 65 6e 20 61 20 62 75 66 66 65 72 0a 23 20 20  hen a buffer.#  
0710: 20 6f 76 65 72 72 65 61 64 20 6d 61 79 20 6f 63   overread may oc
0720: 63 75 72 2e 20 54 68 69 73 20 74 65 73 74 20 73  cur. This test s
0730: 63 72 69 70 74 20 63 72 65 61 74 65 73 20 61 6e  cript creates an
0740: 64 20 6f 70 65 72 61 74 65 73 20 6f 6e 20 76 61  d operates on va
0750: 72 69 6f 75 73 0a 23 20 20 20 73 74 72 61 74 65  rious.#   strate
0760: 67 69 63 61 6c 6c 79 20 63 6f 72 72 75 70 74 65  gically corrupte
0770: 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  d database files
0780: 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 70   to attempt to p
0790: 72 6f 76 6f 6b 65 20 73 75 63 68 20 62 75 66 66  rovoke such buff
07a0: 65 72 0a 23 20 20 20 6f 76 65 72 72 65 61 64 73  er.#   overreads
07b0: 2e 0a 23 0a 23 20 20 20 56 65 72 79 20 6f 66 74  ..#.#   Very oft
07c0: 65 6e 2c 20 61 20 62 75 66 66 65 72 20 6f 76 65  en, a buffer ove
07d0: 72 72 65 61 64 20 70 61 73 73 65 73 20 75 6e 6e  rread passes unn
07e0: 6f 74 69 63 65 64 2c 20 70 61 72 74 69 63 75 6c  oticed, particul
07f0: 61 72 6c 79 20 69 6e 20 77 6f 72 6b 73 74 61 74  arly in workstat
0800: 69 6f 6e 0a 23 20 20 20 65 6e 76 69 72 6f 6e 6d  ion.#   environm
0810: 65 6e 74 73 2e 20 46 6f 72 20 74 68 69 73 20 72  ents. For this r
0820: 65 61 73 6f 6e 2c 20 74 68 69 73 20 74 65 73 74  eason, this test
0830: 20 73 63 72 69 70 74 20 73 68 6f 75 6c 64 20 62   script should b
0840: 65 20 72 75 6e 20 75 73 69 6e 67 20 76 61 6c 67  e run using valg
0850: 72 69 6e 64 0a 23 20 20 20 28 6f 72 20 73 69 6d  rind.#   (or sim
0860: 69 6c 61 72 29 20 69 6e 20 6f 72 64 65 72 20 74  ilar) in order t
0870: 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 6e 6f  o verify that no
0880: 20 6f 76 65 72 72 65 61 64 73 20 6f 63 63 75 72   overreads occur
0890: 2e 0a 23 0a 23 20 54 45 53 54 20 50 4c 41 4e 0a  ..#.# TEST PLAN.
08a0: 23 20 0a 23 20 20 20 54 65 73 74 20 63 61 73 65  # .#   Test case
08b0: 73 20 63 6f 72 72 75 70 74 44 2d 31 2e 2a 20 61  s corruptD-1.* a
08c0: 72 65 20 77 68 69 74 65 2d 62 6f 78 20 74 65 73  re white-box tes
08d0: 74 73 2e 20 54 68 65 79 20 61 74 74 65 6d 70 74  ts. They attempt
08e0: 20 74 6f 20 63 6f 72 72 75 70 74 0a 23 20 20 20   to corrupt.#   
08f0: 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65  one of the above
0900: 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20 65 78   fields, then ex
0910: 65 72 63 69 73 65 20 65 61 63 68 20 70 61 72 74  ercise each part
0920: 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20   of the code in 
0930: 62 74 72 65 65 2e 63 0a 23 20 20 20 74 68 61 74  btree.c.#   that
0940: 20 75 73 65 73 20 73 61 69 64 20 66 69 65 6c 64   uses said field
0950: 2e 0a 23 20 20 20 0a 23 20 20 20 4f 66 66 73 65  ..#   .#   Offse
0960: 74 20 76 61 72 69 61 62 6c 65 73 20 31 2c 20 32  t variables 1, 2
0970: 2c 20 33 20 61 6e 64 20 34 20 61 72 65 20 61 6c  , 3 and 4 are al
0980: 6c 20 63 68 65 63 6b 65 64 20 74 6f 20 6d 61 6b  l checked to mak
0990: 65 20 73 75 72 65 20 74 68 65 79 0a 23 20 20 20  e sure they.#   
09a0: 77 69 6c 6c 20 6e 6f 74 20 72 65 73 75 6c 74 20  will not result 
09b0: 69 6e 20 62 75 66 66 65 72 20 6f 76 65 72 72 75  in buffer overru
09c0: 6e 73 20 61 73 20 70 61 72 74 20 6f 66 20 70 61  ns as part of pa
09d0: 67 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  ge initializatio
09e0: 6e 20 69 6e 0a 23 20 20 20 73 71 6c 69 74 65 33  n in.#   sqlite3
09f0: 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e  BtreeInitPage().
0a00: 20 4f 66 66 73 65 74 73 20 35 20 61 6e 64 20 36   Offsets 5 and 6
0a10: 20 63 61 6e 6e 6f 74 20 62 65 20 74 65 73 74 65   cannot be teste
0a20: 64 20 61 73 20 70 61 72 74 20 6f 66 0a 23 20 20  d as part of.#  
0a30: 20 70 61 67 65 20 69 6e 69 74 69 61 6c 69 7a 61   page initializa
0a40: 74 69 6f 6e 2c 20 61 73 20 74 72 79 69 6e 67 20  tion, as trying 
0a50: 74 6f 20 64 6f 20 73 6f 20 63 61 75 73 65 73 20  to do so causes 
0a60: 61 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 68 69  a performance hi
0a70: 74 2e 0a 23 0a 0a 64 6f 5f 74 65 73 74 20 63 6f  t..#..do_test co
0a80: 72 72 75 70 74 44 2d 31 2e 30 20 7b 0a 20 20 65  rruptD-1.0 {.  e
0a90: 78 65 63 73 71 6c 20 7b 20 0a 20 20 20 20 50 52  xecsql { .    PR
0aa0: 41 47 4d 41 20 61 75 74 6f 5f 76 61 63 75 75 6d  AGMA auto_vacuum
0ab0: 20 3d 20 30 3b 0a 20 20 20 20 50 52 41 47 4d 41   = 0;.    PRAGMA
0ac0: 20 70 61 67 65 5f 73 69 7a 65 20 3d 20 31 30 32   page_size = 102
0ad0: 34 3b 0a 20 20 20 20 43 52 45 41 54 45 20 54 41  4;.    CREATE TA
0ae0: 42 4c 45 20 74 31 28 61 2c 20 62 29 3b 0a 20 20  BLE t1(a, b);.  
0af0: 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69    CREATE INDEX i
0b00: 31 20 4f 4e 20 74 31 28 61 2c 20 62 29 3b 0a 20  1 ON t1(a, b);. 
0b10: 20 7d 0a 20 20 66 6f 72 20 7b 73 65 74 20 69 69   }.  for {set ii
0b20: 20 31 7d 20 7b 24 69 69 20 3c 20 35 30 7d 20 7b   1} {$ii < 50} {
0b30: 69 6e 63 72 20 69 69 7d 20 7b 0a 20 20 20 20 65  incr ii} {.    e
0b40: 78 65 63 73 71 6c 20 7b 20 49 4e 53 45 52 54 20  xecsql { INSERT 
0b50: 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 24  INTO t1 VALUES($
0b60: 69 69 2c 20 24 69 69 20 2a 20 24 69 69 29 20 7d  ii, $ii * $ii) }
0b70: 0a 20 20 7d 0a 20 20 65 78 65 63 73 71 6c 20 7b  .  }.  execsql {
0b80: 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d  .    DELETE FROM
0b90: 20 74 31 20 57 48 45 52 45 20 61 20 3d 20 31 30   t1 WHERE a = 10
0ba0: 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  ;.    DELETE FRO
0bb0: 4d 20 74 31 20 57 48 45 52 45 20 61 20 3d 20 32  M t1 WHERE a = 2
0bc0: 30 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46 52  0;.    DELETE FR
0bd0: 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 3d 20  OM t1 WHERE a = 
0be0: 33 30 3b 0a 20 20 20 20 44 45 4c 45 54 45 20 46  30;.    DELETE F
0bf0: 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 3d  ROM t1 WHERE a =
0c00: 20 34 30 3b 0a 20 20 7d 0a 20 20 63 6f 70 79 5f   40;.  }.  copy_
0c10: 66 69 6c 65 20 74 65 73 74 2e 64 62 20 74 65 73  file test.db tes
0c20: 74 2e 62 75 0a 7d 20 7b 7d 0a 0a 70 72 6f 63 20  t.bu.} {}..proc 
0c30: 69 6e 63 72 5f 63 68 61 6e 67 65 5f 63 6f 75 6e  incr_change_coun
0c40: 74 65 72 20 7b 7d 20 7b 0a 20 20 68 65 78 69 6f  ter {} {.  hexio
0c50: 5f 77 72 69 74 65 20 74 65 73 74 2e 64 62 20 32  _write test.db 2
0c60: 34 20 5b 0a 20 20 20 20 68 65 78 69 6f 5f 72 65  4 [.    hexio_re
0c70: 6e 64 65 72 5f 69 6e 74 33 32 20 5b 65 78 70 72  nder_int32 [expr
0c80: 20 5b 68 65 78 69 6f 5f 67 65 74 5f 69 6e 74 20   [hexio_get_int 
0c90: 5b 68 65 78 69 6f 5f 72 65 61 64 20 74 65 73 74  [hexio_read test
0ca0: 2e 64 62 20 32 34 20 34 5d 5d 20 2b 20 31 5d 0a  .db 24 4]] + 1].
0cb0: 20 20 5d 0a 7d 0a 0a 70 72 6f 63 20 72 65 73 74    ].}..proc rest
0cc0: 6f 72 65 5f 66 69 6c 65 20 7b 7d 20 7b 0a 20 20  ore_file {} {.  
0cd0: 64 62 20 63 6c 6f 73 65 0a 20 20 63 6f 70 79 5f  db close.  copy_
0ce0: 66 69 6c 65 20 74 65 73 74 2e 62 75 20 74 65 73  file test.bu tes
0cf0: 74 2e 64 62 0a 20 20 73 71 6c 69 74 65 33 20 64  t.db.  sqlite3 d
0d00: 62 20 74 65 73 74 2e 64 62 0a 7d 0a 0a 23 2d 2d  b test.db.}..#--
0d10: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d20: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d30: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0d50: 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 65 20 66 6f  -------.# The fo
0d60: 6c 6c 6f 77 69 6e 67 20 74 65 73 74 73 2c 20 63  llowing tests, c
0d70: 6f 72 72 75 70 74 44 2d 31 2e 31 2e 2a 2c 20 66  orruptD-1.1.*, f
0d80: 6f 63 75 73 20 6f 6e 20 74 68 65 20 70 61 67 65  ocus on the page
0d90: 20 68 65 61 64 65 72 20 66 69 65 6c 64 0a 23 20   header field.# 
0da0: 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f  containing the o
0db0: 66 66 73 65 74 20 6f 66 20 74 68 65 20 66 69 72  ffset of the fir
0dc0: 73 74 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 6e  st free block in
0dd0: 20 61 20 70 61 67 65 2e 20 0a 23 0a 64 6f 5f 74   a page. .#.do_t
0de0: 65 73 74 20 63 6f 72 72 75 70 74 44 2d 31 2e 31  est corruptD-1.1
0df0: 2e 31 20 7b 0a 20 20 69 6e 63 72 5f 63 68 61 6e  .1 {.  incr_chan
0e00: 67 65 5f 63 6f 75 6e 74 65 72 0a 20 20 68 65 78  ge_counter.  hex
0e10: 69 6f 5f 77 72 69 74 65 20 74 65 73 74 2e 64 62  io_write test.db
0e20: 20 5b 65 78 70 72 20 31 30 32 34 2b 31 5d 20 46   [expr 1024+1] F
0e30: 46 46 46 0a 20 20 63 61 74 63 68 73 71 6c 20 7b  FFF.  catchsql {
0e40: 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
0e50: 31 20 7d 0a 7d 20 7b 31 20 7b 64 61 74 61 62 61  1 }.} {1 {databa
0e60: 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20 69 73  se disk image is
0e70: 20 6d 61 6c 66 6f 72 6d 65 64 7d 7d 0a 64 6f 5f   malformed}}.do_
0e80: 74 65 73 74 20 63 6f 72 72 75 70 74 44 2d 31 2e  test corruptD-1.
0e90: 31 2e 32 20 7b 0a 20 20 69 6e 63 72 5f 63 68 61  1.2 {.  incr_cha
0ea0: 6e 67 65 5f 63 6f 75 6e 74 65 72 0a 20 20 68 65  nge_counter.  he
0eb0: 78 69 6f 5f 77 72 69 74 65 20 74 65 73 74 2e 64  xio_write test.d
0ec0: 62 20 5b 65 78 70 72 20 31 30 32 34 2b 31 5d 20  b [expr 1024+1] 
0ed0: 5b 68 65 78 69 6f 5f 72 65 6e 64 65 72 5f 69 6e  [hexio_render_in
0ee0: 74 33 32 20 31 30 32 31 5d 0a 20 20 63 61 74 63  t32 1021].  catc
0ef0: 68 73 71 6c 20 7b 20 53 45 4c 45 43 54 20 2a 20  hsql { SELECT * 
0f00: 46 52 4f 4d 20 74 31 20 7d 0a 7d 20 7b 31 20 7b  FROM t1 }.} {1 {
0f10: 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d  database disk im
0f20: 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64  age is malformed
0f30: 7d 7d 0a 0a 23 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  }}..#-----------
0f40: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f50: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f60: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
0f70: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 23  --------------.#
0f80: 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
0f90: 65 73 74 73 2c 20 63 6f 72 72 75 70 74 44 2d 31  ests, corruptD-1
0fa0: 2e 32 2e 2a 2c 20 66 6f 63 75 73 20 6f 6e 20 74  .2.*, focus on t
0fb0: 68 65 20 6f 66 66 73 65 74 73 20 63 6f 6e 74 61  he offsets conta
0fc0: 69 6e 65 64 0a 23 20 69 6e 20 74 68 65 20 66 69  ined.# in the fi
0fd0: 72 73 74 20 32 20 62 79 74 65 20 6f 66 20 65 61  rst 2 byte of ea
0fe0: 63 68 20 66 72 65 65 2d 62 6c 6f 63 6b 20 6f 6e  ch free-block on
0ff0: 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a   the free-list..
1000: 23 0a 64 6f 5f 74 65 73 74 20 63 6f 72 72 75 70  #.do_test corrup
1010: 74 44 2d 31 2e 32 2e 31 20 7b 0a 20 20 72 65 73  tD-1.2.1 {.  res
1020: 74 6f 72 65 5f 66 69 6c 65 0a 7d 20 7b 7d 0a 64  tore_file.} {}.d
1030: 6f 5f 74 65 73 74 20 63 6f 72 72 75 70 74 44 2d  o_test corruptD-
1040: 31 2e 32 2e 32 20 7b 0a 7d 20 7b 7d 0a 0a 23 2d  1.2.2 {.} {}..#-
1050: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1060: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1070: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1080: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1090: 2d 2d 2d 2d 2d 2d 2d 2d 0a 23 20 54 68 65 20 66  --------.# The f
10a0: 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74 73 2c 20  ollowing tests, 
10b0: 63 6f 72 72 75 70 74 44 2d 31 2e 34 2e 2a 2c 20  corruptD-1.4.*, 
10c0: 2e 2e 2e 0a 23 0a 0a 0a 23 2d 2d 2d 2d 2d 2d 2d  ....#...#-------
10d0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10e0: 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
10f0: 2d 2d 2d 2d 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 0a 23 20 54 68 65 20 66 6f 6c 6c 6f 77 69  --.# The followi
1120: 6e 67 20 74 65 73 74 73 2c 20 63 6f 72 72 75 70  ng tests, corrup
1130: 74 44 2d 31 2e 35 2e 2a 2c 20 66 6f 63 75 73 20  tD-1.5.*, focus 
1140: 6f 6e 20 74 68 65 20 6f 66 66 73 65 74 73 20 63  on the offsets c
1150: 6f 6e 74 61 69 6e 65 64 0a 23 20 69 6e 20 74 68  ontained.# in th
1160: 65 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61 72  e cell offset ar
1170: 72 61 79 2e 0a 23 20 0a 23 20 20 20 64 65 66 72  ray..# .#   defr
1180: 61 67 6d 65 6e 74 50 61 67 65 0a 23 0a 0a 66 69  agmentPage.#..fi
1190: 6e 69 73 68 5f 74 65 73 74 0a                    nish_test.