Skip to content

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

Open
maflcko wants to merge 4 commits into
bitcoin:masterfrom
maflcko:2611-test-CompactSize-MAX_SIZE
Open

maflcko wants to merge 4 commits into
bitcoin:masterfrom
maflcko:2611-test-CompactSize-MAX_SIZE

Conversation

@maflcko

@maflcko maflcko commented Nov 13, 2026 •

Copy link
Copy Markdown
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:

@DrahtBot DrahtBot changed the title test: Add test for CompactSize larger than MAX_SIZE value test: Add test for CompactSize larger than MAX_SIZE value Nov 13, 2026
@DrahtBot DrahtBot added the Tests label Nov 13, 2026
@DrahtBot

DrahtBot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

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

Type Reviewers
ACK seduless, m3dwards

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36918 (Witness stacks in serialized form (optimization follow-ups) by sipa)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko

maflcko commented Nov 13, 2026

Copy link
Copy Markdown
Member Author

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;

@m3dwards

Copy link
Copy Markdown
Contributor

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

Concept ACK

@m3dwards

Copy link
Copy Markdown
Contributor

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

Script
#!/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

@maflcko

maflcko commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

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.

Comment thread src/test/serialize_tests.cpp Resolved
Comment thread src/test/serialize_tests.cpp Resolved
Comment thread src/test/serialize_tests.cpp Resolved
Comment thread test/functional/p2p_invalid_messages.py Resolved
* Replace the isCanonicalException impl with the normal pattern of
  HasReason
* Drop the confusing std::vector::size_t, becasue ReadCompactSize
  returns an u64.
@maflcko
maflcko force-pushed the 2611-test-CompactSize-MAX_SIZE branch from fa3f8f9 to fa01715 Compare November 19, 2026 14:01
@maflcko

maflcko commented Nov 19, 2026

Copy link
Copy Markdown
Member Author

thx, took all nits

Comment thread test/functional/p2p_invalid_messages.py Resolved
@m3dwards

Copy link
Copy Markdown
Contributor

ACK fa01715

@seduless seduless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK fa5aabe

  • Verified vs the PR's base no unit or functional test catches the mutation to delete the guard
  • Verified the new unit (compactsize_value_range) and functional (test_corrupt_compactsize) tests fail when applying the same mutation

@DrahtBot
DrahtBot requested a review from m3dwards November 23, 2026 21:23
@m3dwards

Copy link
Copy Markdown
Contributor

reACK fa5aabe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants