Repository navigation
wire: document that ReadVarInt does not bound the returned value - #2681
Closed
amalsaidov wants to merge 1 commit into
Closed
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
ReadVarInt only checks that a CompactSize is canonically encoded. It applies no upper bound, so it returns any canonical value up to 2^64-1. Bitcoin Core's ReadCompactSize rejects values above MAX_SIZE (0x02000000) by default, so an external caller that uses ReadVarInt for an element count without bounding the result itself diverges from Core. Document the contract on ReadVarInt and ReadVarIntBuf so callers know they must validate the count against their own limit, and add a regression test pinning that above-MAX_SIZE values decode without error. AI-assisted; directed, reviewed and tested by the author.
allocz
reviewed
Nov 12, 2026
Comment on lines
+615
to
+621
| // | ||
| // The result is only checked for canonical (minimal) encoding; it is not range | ||
| // checked against an upper bound. Unlike Bitcoin Core's ReadCompactSize, which | ||
| // rejects values greater than MAX_SIZE (0x02000000) by default, this function | ||
| // returns any canonically encoded value up to 2^64-1. Callers that use the | ||
| // result as an element count must validate it against their own limit (for | ||
| // example MaxMessagePayload) before allocating or reading that many elements. |
Contributor
There was a problem hiding this comment.
By reading the function signature we can already assume that any integer from 0 to MAX_UINT64 should be valid, so I don't see a reason to add more docs. Also, the test is redundant as we already have test covering the exact same code path. Which LLM have you used to generate this PR?
Author
|
Fair points, thanks for the review. To answer your question: it was written with Claude (Claude Code), directed and reviewed by me. I agree that the signature already implies the full uint64 range and the existing test covers this path, so I'm closing this. Sorry for the noise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2638.
ReadVarIntonly checks that a CompactSize is canonically encoded. It applies no upper bound, so it returns any canonical value up to 2^64-1. Bitcoin Core'sReadCompactSizerejects values aboveMAX_SIZE(0x02000000) by default, so an external caller that usesReadVarIntfor an element count without bounding the result itself ends up more lenient than Core.Every message decoder in the tree already bounds the count it reads (
ReadVarStringagainstMaxMessagePayload,MsgTx.BtcDecodeagainst its own limits, and so on), so no message decoder changes behavior here. The gap is only visible to callers that use the exported function directly.Changing the return value would break in-tree callers that rely on the current behavior, so this documents the contract instead. It spells out on
ReadVarIntandReadVarIntBufthat the result is not range checked and that callers must validate the count against their own limit. It also addsTestVarIntUnboundedto pin that above-MAX_SIZEvalues decode without error, which guards the documented behavior against accidental changes.Build and tests (run inside the
wiremodule):All pass with no diff.
AI-assisted; directed, reviewed and tested by the author.