test: Add test for CompactSize larger than MAX_SIZE value #36853

pull maflcko wants to merge 4 commits into bitcoin:master from maflcko:2611-test-CompactSize-MAX_SIZE changing 3 files +40 −39
  1. maflcko commented at 11:11 AM on November 13, 2026: member

    Currently there is no test coverage for oversized CompactSize. This is fine, because a vector this large will already be rejected earlier by the P2P transport payload size check.

    However, it would be good to still add test coverage:

  2. test: refactor non-canonical CompactSize test
    * Replace the isCanonicalException impl with the normal pattern of
      HasReason
    * Drop the confusing std::vector::size_t, becasue ReadCompactSize
      returns a u64.
    fa7b86746d
  3. test: Add test for CompactSize larger than MAX_SIZE value fa59bdbc57
  4. refactor: clang-format Read/Write CompactSize
    Dropt the extaneous 15 lines. Can be reviewed with the git option
    --word-diff-regex=.
    fa9f35453f
  5. test: Add P2P test for CompactSize > MAX_SIZE fa3f8f9a2a
  6. DrahtBot renamed this:
    test: Add test for CompactSize larger than MAX_SIZE value
    test: Add test for CompactSize larger than MAX_SIZE value
    on Nov 13, 2026
  7. DrahtBot added the label Tests on Nov 13, 2026
  8. DrahtBot commented at 11:11 AM on November 13, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept ACK m3dwards

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  9. maflcko commented at 11:13 AM on November 13, 2026: member

    Mutation:

    diff --git a/src/serialize.h b/src/serialize.h
    index 9a1ff56ec4..81d675c3c0 100644
    --- a/src/serialize.h
    +++ b/src/serialize.h
    @@ -358,5 +358,2 @@ uint64_t ReadCompactSize(Stream& is, bool range_check = true)
         }
    -    if (range_check && nSizeRet > MAX_SIZE) {
    -        throw std::ios_base::failure("ReadCompactSize(): size too large");
    -    }
         return nSizeRet;
    

    <!--- +BOOST_AUTO_TEST_CASE(compactsize_type_range) +{ + using Fmt = CompactSizeFormatter</*RangeCheck=*/false>; // Only type check + DataStream ss{}; + ss << Using<Fmt>(uint64_t{std::numeric_limits<uint32_t>::max()} + 1); + uint32_t value; + BOOST_CHECK_EXCEPTION(ss >> Using<Fmt>(value), std::ios_base::failure, HasReason{"CompactSize exceeds limit of type"}); +}

  10. m3dwards commented at 2:30 PM on November 13, 2026: contributor

    Quite nuanced to track down! Fixing this will help with coverage determinism.

    Concept ACK

  11. m3dwards commented at 5:39 PM on November 16, 2026: contributor

    If this is useful to anyone, here a script that reproduces the non-determinism in coverage. With this PR applied it is fixed.

    <details> <summary>Script</summary>

    #!/usr/bin/env bash
    # Coverage check for the src/serialize.h "ReadCompactSize(): size too large" guard.
    # Builds with LLVM coverage, then runs the unit suite's serialisation cases and one
    # functional test per run, and reports the guard's execution count for each run.
    # Run from the bitcoin checkout root. Needs clang, cmake, python3, llvm-profdata and
    # llvm-cov on PATH. Usage: ./guard-flip.sh [runs]      (default 8)
    set -eu
    cd "$(dirname "$0")"
    RUNS=${1:-8}
    
    echo 'Building bitcoin with coverage'
    
    cmake -B build -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
      -DAPPEND_CFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
      -DAPPEND_CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
      -DAPPEND_LDFLAGS="-fprofile-instr-generate" \
      -DBUILD_GUI=OFF -DBUILD_BENCH=OFF -DBUILD_FUZZ_BINARY=OFF >/dev/null
    cmake --build build -j"$(nproc)" --target bitcoind bitcoin-cli test_bitcoin >/dev/null
    
    L=$(grep -n 'ReadCompactSize(): size too large' src/serialize.h | cut -d: -f1)
    echo "guard line: src/serialize.h:$L"
    counts=()
    for i in $(seq "$RUNS"); do
      d=$(mktemp -d)
      LLVM_PROFILE_FILE=$d/u_%m_%p.profraw ./build/bin/test_bitcoin --run_test=serialize_tests >/dev/null 2>&1 || true
      LLVM_PROFILE_FILE=$d/f_%m_%p.profraw TMPDIR=/tmp \
        python3 build/test/functional/test_runner.py wallet_create_tx.py --randomseed=1234 >/dev/null 2>&1 || true
      llvm-profdata merge -o "$d/p.profdata" "$d"/*.profraw 2>/dev/null
      llvm-cov export --format=lcov --object=build/bin/bitcoind --object=build/bin/test_bitcoin \
        --instr-profile="$d/p.profdata" > "$d/info" 2>"$d/err" || true
      c=$(awk -F'[:,]' -v l="$L" '/^SF:.*serialize\.h$/ {s=1} s && $1=="DA" && $2==l {print $3; exit}' "$d/info")
      [ -n "$c" ] || { echo "run $i: no DA record for line $L, see $d/err" >&2; exit 2; }
      counts+=("$c")
      echo "run $i: $c"
      rm -rf "$d"
    done
    echo "counts: ${counts[*]}"
    if printf '%s\n' "${counts[@]}" | grep -qx 0; then
      echo "FLIPS: the guard was left uncovered on at least one run"
    else
      echo "NO FLIPS: the guard was covered on every run"
    fi
    
    

    </details>

  12. maflcko commented at 9:32 AM on November 17, 2026: member

    If this is useful to anyone, here a script that reproduces the non-determinism in coverage.

    An alternative would be to just replace the code with assert(false) and then run the functional tests. Sometimes they pass, sometimes they fail.

Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-18 16:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me