(command.CommandList
  children: [
    (command.ShAssignment
      pairs: [
        (assign_pair
          lhs: (sh_lhs_expr.Name name:test_description)
          op: assign_op.Equal
          rhs: 
            {
              (SQ 
                (Token
                  id: Id.Lit_Chars
                  val: 'bounds-checking of access to mmapped on-disk file formats'
                  span_id: 6
                )
              )
            }
          spids: [4]
        )
      ]
    )
    (C {(.)} {(./test-lib.sh)})
    (command.ShFunction
      name: clear_base
      body: 
        (command.BraceGroup
          children: [
            (command.AndOr
              ops: [Id.Op_DAmp]
              children: [
                (C {(test_when_finished)} {(SQ (Token id:Id.Lit_Chars val:restore_base span_id:25))})
                (C {(rm)} {(-f)} {($ Id.VSub_DollarName '$base')})
              ]
            )
          ]
        )
    )
    (command.ShFunction
      name: restore_base
      body: 
        (command.BraceGroup
          children: [(C {(cp)} {(base-backup/) (Id.Lit_Star '*')} {(.git/objects/pack/)})]
        )
    )
    (command.ShFunction
      name: do_pack
      body: 
        (command.BraceGroup
          children: [
            (command.Sentence
              child: 
                (command.ShAssignment
                  pairs: [
                    (assign_pair
                      lhs: (sh_lhs_expr.Name name:pack_objects)
                      op: assign_op.Equal
                      rhs: {($ Id.VSub_Number '$1')}
                      spids: [66]
                    )
                  ]
                )
              terminator: (Token id:Id.Op_Semi val:';' span_id:68)
            )
            (C {(shift)})
            (command.AndOr
              ops: [Id.Op_DAmp Id.Op_DAmp Id.Op_DAmp Id.Op_DAmp]
              children: [
                (command.ShAssignment
                  pairs: [
                    (assign_pair
                      lhs: (sh_lhs_expr.Name name:sha1)
                      op: assign_op.Equal
                      rhs: 
                        {
                          (command_sub
                            left_token: (Token id:Id.Left_DollarParen val:'$(' span_id:74)
                            command_list: 
                              (command.CommandList
                                children: [
                                  (command.Pipeline
                                    children: [
                                      (command.ForEach
                                        iter_name: i
                                        iter_words: [{($ Id.VSub_DollarName '$pack_objects')}]
                                        do_arg_iter: F
                                        body: 
                                          (command.DoGroup
                                            children: [(C {(echo)} {($ Id.VSub_DollarName '$i')})]
                                          )
                                      )
                                      (C {(git)} {(pack-objects)} {(DQ ($ Id.VSub_At '$@'))} 
                                        {(.git/objects/pack/pack)}
                                      )
                                    ]
                                    negated: F
                                  )
                                ]
                              )
                          )
                        }
                      spids: [73]
                    )
                  ]
                )
                (command.ShAssignment
                  pairs: [
                    (assign_pair
                      lhs: (sh_lhs_expr.Name name:pack)
                      op: assign_op.Equal
                      rhs: {(.git/objects/pack/pack-) ($ Id.VSub_DollarName '$sha1') (.pack)}
                      spids: [114]
                    )
                  ]
                )
                (command.ShAssignment
                  pairs: [
                    (assign_pair
                      lhs: (sh_lhs_expr.Name name:idx)
                      op: assign_op.Equal
                      rhs: {(.git/objects/pack/pack-) ($ Id.VSub_DollarName '$sha1') (.idx)}
                      spids: [122]
                    )
                  ]
                )
                (C {(chmod)} {(Id.Lit_Other '+') (w)} {($ Id.VSub_DollarName '$pack')} 
                  {($ Id.VSub_DollarName '$idx')}
                )
                (C {(test_when_finished)} 
                  {(SQ (Token id:Id.Lit_Chars val:'rm -f "$pack" "$idx"' span_id:145))}
                )
              ]
            )
          ]
        )
    )
    (command.ShFunction
      name: munge
      body: 
        (command.BraceGroup
          children: [
            (command.Pipeline
              children: [
                (C {(printf)} {(DQ ($ Id.VSub_Number '$3'))})
                (C {(dd)} {(Id.Lit_VarLike 'of=') (DQ ($ Id.VSub_Number '$1'))} 
                  {(Id.Lit_VarLike 'bs=') (1)} {(Id.Lit_VarLike 'conv=') (notrunc)} {(Id.Lit_VarLike 'seek=') ($ Id.VSub_Number '$2')}
                )
              ]
              negated: F
            )
          ]
        )
    )
    (command.ShFunction
      name: ofs_table
      body: 
        (command.BraceGroup
          children: [
            (C {(echo)} 
              {
                (word_part.ArithSub
                  anode: 
                    (arith_expr.Binary
                      op_id: Id.Arith_Plus
                      left: 
                        (arith_expr.Binary
                          op_id: Id.Arith_Plus
                          left: 
                            (arith_expr.Binary
                              op_id: Id.Arith_Plus
                              left: 
                                (arith_expr.Binary
                                  op_id: Id.Arith_Plus
                                  left: (arith_expr.ArithWord w:{(Id.Lit_Digits 4)})
                                  right: (arith_expr.ArithWord w:{(Id.Lit_Digits 4)})
                                )
                              right: 
                                (arith_expr.Binary
                                  op_id: Id.Arith_Star
                                  left: (arith_expr.ArithWord w:{(Id.Lit_Digits 4)})
                                  right: (arith_expr.ArithWord w:{(Id.Lit_Digits 256)})
                                )
                            )
                          right: 
                            (arith_expr.Binary
                              op_id: Id.Arith_Star
                              left: (arith_expr.ArithWord w:{(Id.Lit_Digits 20)})
                              right: (arith_expr.ArithWord w:{($ Id.VSub_Number '$1')})
                            )
                        )
                      right: 
                        (arith_expr.Binary
                          op_id: Id.Arith_Star
                          left: (arith_expr.ArithWord w:{(Id.Lit_Digits 4)})
                          right: (arith_expr.ArithWord w:{($ Id.VSub_Number '$1')})
                        )
                    )
                )
              }
            )
          ]
        )
    )
    (command.ShFunction
      name: extended_table
      body: 
        (command.BraceGroup
          children: [
            (C {(echo)} 
              {
                (word_part.ArithSub
                  anode: 
                    (arith_expr.Binary
                      op_id: Id.Arith_Plus
                      left: 
                        (arith_expr.ArithWord
                          w: 
                            {
                              (command_sub
                                left_token: (Token id:Id.Left_DollarParen val:'$(' span_id:257)
                                command_list: 
                                  (command.CommandList
                                    children: [(C {(ofs_table)} {(DQ ($ Id.VSub_Number '$1'))})]
                                  )
                              )
                            }
                        )
                      right: 
                        (arith_expr.Binary
                          op_id: Id.Arith_Star
                          left: (arith_expr.ArithWord w:{(Id.Lit_Digits 4)})
                          right: (arith_expr.ArithWord w:{($ Id.VSub_Number '$1')})
                        )
                    )
                )
              }
            )
          ]
        )
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'set up base packfile and variables' span_id:279))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:283) 
          (Token
            id: Id.Lit_Chars
            val: '\t# the hash of this content starts with ff, which\n'
            span_id: 284
          ) (Token id:Id.Lit_Chars val:'\t# makes some later computations much simpler\n' span_id:285) 
          (Token id:Id.Lit_Chars val:'\techo 74 >file &&\n' span_id:286) (Token id:Id.Lit_Chars val:'\tgit add file &&\n' span_id:287) 
          (Token id:Id.Lit_Chars val:'\tgit commit -m base &&\n' span_id:288) (Token id:Id.Lit_Chars val:'\tgit repack -ad &&\n' span_id:289) 
          (Token id:Id.Lit_Chars val:'\tbase=$(echo .git/objects/pack/*) &&\n' span_id:290) (Token id:Id.Lit_Chars val:'\tchmod +w $base &&\n' span_id:291) 
          (Token id:Id.Lit_Chars val:'\tmkdir base-backup &&\n' span_id:292) (Token id:Id.Lit_Chars val:'\tcp $base base-backup/ &&\n' span_id:293) 
          (Token id:Id.Lit_Chars val:'\tobject=$(git rev-parse HEAD:file)\n' span_id:294)
        )
      }
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'pack/index object count mismatch' span_id:301))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:305) 
          (Token id:Id.Lit_Chars val:'\tdo_pack $object &&\n' span_id:306) (Token id:Id.Lit_Chars val:'\tmunge $pack 8 "\\377\\0\\0\\0" &&\n' span_id:307) 
          (Token id:Id.Lit_Chars val:'\tclear_base &&\n' span_id:308) (Token id:Id.Lit_Chars val:'\n' span_id:309) 
          (Token
            id: Id.Lit_Chars
            val: '\t# We enumerate the objects from the completely-fine\n'
            span_id: 310
          ) (Token id:Id.Lit_Chars val:'\t# .idx, but notice later that the .pack is bogus\n' span_id:311) 
          (Token id:Id.Lit_Chars val:'\t# and fail to show any data.\n' span_id:312) (Token id:Id.Lit_Chars val:'\techo "$object missing" >expect &&\n' span_id:313) 
          (Token
            id: Id.Lit_Chars
            val: '\tgit cat-file --batch-all-objects --batch-check >actual &&\n'
            span_id: 314
          ) (Token id:Id.Lit_Chars val:'\ttest_cmp expect actual &&\n' span_id:315) 
          (Token id:Id.Lit_Chars val:'\n' span_id:316) 
          (Token
            id: Id.Lit_Chars
            val: '\t# ...and here fail to load the object (without segfaulting),\n'
            span_id: 317
          ) (Token id:Id.Lit_Chars val:'\t# but fallback to a good copy if available.\n' span_id:318) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git cat-file blob $object &&\n'
            span_id: 319
          ) (Token id:Id.Lit_Chars val:'\trestore_base &&\n' span_id:320) 
          (Token
            id: Id.Lit_Chars
            val: '\tgit cat-file blob $object >actual &&\n'
            span_id: 321
          ) (Token id:Id.Lit_Chars val:'\ttest_cmp file actual &&\n' span_id:322) 
          (Token id:Id.Lit_Chars val:'\n' span_id:323) 
          (Token
            id: Id.Lit_Chars
            val: '\t# ...and make sure that index-pack --verify, which has its\n'
            span_id: 324
          ) (Token id:Id.Lit_Chars val:'\t# own reading routines, does not segfault.\n' span_id:325) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git index-pack --verify $pack\n'
            span_id: 326
          )
        )
      }
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'matched bogus object count' span_id:333))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:337) 
          (Token id:Id.Lit_Chars val:'\tdo_pack $object &&\n' span_id:338) (Token id:Id.Lit_Chars val:'\tmunge $pack 8 "\\377\\0\\0\\0" &&\n' span_id:339) 
          (Token
            id: Id.Lit_Chars
            val: '\tmunge $idx $((255 * 4)) "\\377\\0\\0\\0" &&\n'
            span_id: 340
          ) (Token id:Id.Lit_Chars val:'\tclear_base &&\n' span_id:341) 
          (Token id:Id.Lit_Chars val:'\n' span_id:342) 
          (Token
            id: Id.Lit_Chars
            val: '\t# Unlike above, we should notice early that the .idx is totally\n'
            span_id: 343
          ) (Token id:Id.Lit_Chars val:'\t# bogus, and not even enumerate its contents.\n' span_id:344) 
          (Token id:Id.Lit_Chars val:'\t>expect &&\n' span_id:345) 
          (Token
            id: Id.Lit_Chars
            val: '\tgit cat-file --batch-all-objects --batch-check >actual &&\n'
            span_id: 346
          ) (Token id:Id.Lit_Chars val:'\ttest_cmp expect actual &&\n' span_id:347) 
          (Token id:Id.Lit_Chars val:'\n' span_id:348) 
          (Token
            id: Id.Lit_Chars
            val: '\t# But as before, we can do the same object-access checks.\n'
            span_id: 349
          ) (Token id:Id.Lit_Chars val:'\ttest_must_fail git cat-file blob $object &&\n' span_id:350) 
          (Token id:Id.Lit_Chars val:'\trestore_base &&\n' span_id:351) (Token id:Id.Lit_Chars val:'\tgit cat-file blob $object >actual &&\n' span_id:352) 
          (Token id:Id.Lit_Chars val:'\ttest_cmp file actual &&\n' span_id:353) (Token id:Id.Lit_Chars val:'\n' span_id:354) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git index-pack --verify $pack\n'
            span_id: 355
          )
        )
      }
    )
    (C {(test_expect_success)} {(SQ (Token id:Id.Lit_Chars val:'bogus object offset (v1)' span_id:387))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:391) 
          (Token
            id: Id.Lit_Chars
            val: '\tdo_pack $object --index-version=1 &&\n'
            span_id: 392
          ) (Token id:Id.Lit_Chars val:'\tmunge $idx $((4 * 256)) "\\377\\0\\0\\0" &&\n' span_id:393) 
          (Token id:Id.Lit_Chars val:'\tclear_base &&\n' span_id:394) (Token id:Id.Lit_Chars val:'\ttest_must_fail git cat-file blob $object &&\n' span_id:395) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git index-pack --verify $pack\n'
            span_id: 396
          )
        )
      }
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'bogus object offset (v2, no msb)' span_id:403))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:407) 
          (Token
            id: Id.Lit_Chars
            val: '\tdo_pack $object --index-version=2 &&\n'
            span_id: 408
          ) (Token id:Id.Lit_Chars val:'\tmunge $idx $(ofs_table 1) "\\0\\377\\0\\0" &&\n' span_id:409) 
          (Token id:Id.Lit_Chars val:'\tclear_base &&\n' span_id:410) (Token id:Id.Lit_Chars val:'\ttest_must_fail git cat-file blob $object &&\n' span_id:411) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git index-pack --verify $pack\n'
            span_id: 412
          )
        )
      }
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'bogus offset into v2 extended table' span_id:419))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:423) 
          (Token
            id: Id.Lit_Chars
            val: '\tdo_pack $object --index-version=2 &&\n'
            span_id: 424
          ) (Token id:Id.Lit_Chars val:'\tmunge $idx $(ofs_table 1) "\\377\\0\\0\\0" &&\n' span_id:425) 
          (Token id:Id.Lit_Chars val:'\tclear_base &&\n' span_id:426) (Token id:Id.Lit_Chars val:'\ttest_must_fail git cat-file blob $object &&\n' span_id:427) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git index-pack --verify $pack\n'
            span_id: 428
          )
        )
      }
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'bogus offset inside v2 extended table' span_id:435))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:439) 
          (Token
            id: Id.Lit_Chars
            val: '\t# We need two objects here, so we can plausibly require\n'
            span_id: 440
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\t# an extended table (if the first object were larger than 2^31).\n'
            span_id: 441
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\tdo_pack "$object $(git rev-parse HEAD)" --index-version=2 &&\n'
            span_id: 442
          ) (Token id:Id.Lit_Chars val:'\n' span_id:443) 
          (Token
            id: Id.Lit_Chars
            val: '\t# We have to make extra room for the table, so we cannot\n'
            span_id: 444
          ) (Token id:Id.Lit_Chars val:'\t# just munge in place as usual.\n' span_id:445) 
          (Token id:Id.Lit_Chars val:'\t{\n' span_id:446) (Token id:Id.Lit_Chars val:'\t\tdd if=$idx bs=1 count=$(($(ofs_table 2) + 4)) &&\n' span_id:447) 
          (Token id:Id.Lit_Chars val:'\t\tprintf "\\200\\0\\0\\0" &&\n' span_id:448) (Token id:Id.Lit_Chars val:'\t\tprintf "\\377\\0\\0\\0\\0\\0\\0\\0" &&\n' span_id:449) 
          (Token
            id: Id.Lit_Chars
            val: '\t\tdd if=$idx bs=1 skip=$(extended_table 2)\n'
            span_id: 450
          ) (Token id:Id.Lit_Chars val:'\t} >tmp &&\n' span_id:451) 
          (Token id:Id.Lit_Chars val:'\tmv tmp "$idx" &&\n' span_id:452) (Token id:Id.Lit_Chars val:'\tclear_base &&\n' span_id:453) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git cat-file blob $object &&\n'
            span_id: 454
          ) (Token id:Id.Lit_Chars val:'\ttest_must_fail git index-pack --verify $pack\n' span_id:455)
        )
      }
    )
    (C {(test_expect_success)} 
      {(SQ (Token id:Id.Lit_Chars val:'bogus OFS_DELTA in packfile' span_id:462))} 
      {
        (SQ (Token id:Id.Lit_Chars val:'\n' span_id:466) 
          (Token
            id: Id.Lit_Chars
            val: '\t# Generate a pack with a delta in it.\n'
            span_id: 467
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\tbase=$(test-genrandom foo 3000 | git hash-object --stdin -w) &&\n'
            span_id: 468
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\tdelta=$(test-genrandom foo 2000 | git hash-object --stdin -w) &&\n'
            span_id: 469
          ) (Token id:Id.Lit_Chars val:'\tdo_pack "$base $delta" --delta-base-offset &&\n' span_id:470) 
          (Token id:Id.Lit_Chars val:'\trm -f .git/objects/??/* &&\n' span_id:471) (Token id:Id.Lit_Chars val:'\n' span_id:472) 
          (Token
            id: Id.Lit_Chars
            val: '\t# Double check that we have the delta we expect.\n'
            span_id: 473
          ) (Token id:Id.Lit_Chars val:'\techo $base >expect &&\n' span_id:474) 
          (Token
            id: Id.Lit_Chars
            val: '\techo $delta | git cat-file --batch-check="%(deltabase)" >actual &&\n'
            span_id: 475
          ) (Token id:Id.Lit_Chars val:'\ttest_cmp expect actual &&\n' span_id:476) 
          (Token id:Id.Lit_Chars val:'\n' span_id:477) 
          (Token
            id: Id.Lit_Chars
            val: '\t# Now corrupt it. We assume the varint size for the delta is small\n'
            span_id: 478
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\t# enough to fit in the first byte (which it should be, since it\n'
            span_id: 479
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\t# is a pure deletion from the base), and that original ofs_delta\n'
            span_id: 480
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\t# takes 2 bytes (which it should, as it should be ~3000).\n'
            span_id: 481
          ) 
          (Token
            id: Id.Lit_Chars
            val: '\tofs=$(git show-index <$idx | grep $delta | cut -d" " -f1) &&\n'
            span_id: 482
          ) (Token id:Id.Lit_Chars val:'\tmunge $pack $(($ofs + 1)) "\\177\\377" &&\n' span_id:483) 
          (Token
            id: Id.Lit_Chars
            val: '\ttest_must_fail git cat-file blob $delta >/dev/null\n'
            span_id: 484
          )
        )
      }
    )
    (C {(test_done)})
  ]
)