Skip to content

wire: document that ReadVarInt does not bound the returned value - #2681

Closed
amalsaidov wants to merge 1 commit into
btcsuite:masterfrom
amalsaidov:wire-doc-readvarint-bound
Closed

amalsaidov wants to merge 1 commit into
btcsuite:masterfrom
amalsaidov:wire-doc-readvarint-bound

Conversation

@amalsaidov

Copy link
Copy Markdown

Fixes #2638.

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 ends up more lenient than Core.

Every message decoder in the tree already bounds the count it reads (ReadVarString against MaxMessagePayload, MsgTx.BtcDecode against 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 ReadVarInt and ReadVarIntBuf that the result is not range checked and that callers must validate the count against their own limit. It also adds TestVarIntUnbounded to pin that above-MAX_SIZE values decode without error, which guards the documented behavior against accidental changes.

Build and tests (run inside the wire module):

go build ./...
go test ./...
gofmt -l .

All pass with no diff.

AI-assisted; directed, reviewed and tested by the author.

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.
Comment thread wire/common.go
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.

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.

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?

@amalsaidov

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

@amalsaidov amalsaidov closed this Nov 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wire: ReadVarInt returns values above Bitcoin Core's MAX_SIZE

2 participants