test(StdJson): cover readBytes at parseRaw's inferred-type lengths - #900
Open
developer3516 wants to merge 1 commit into
Open
test(StdJson): cover readBytes at parseRaw's inferred-type lengths#900developer3516 wants to merge 1 commit into
developer3516 wants to merge 1 commit into
Conversation
`readBytes` has no test coverage at all, and the length at which it once broke is still a special case in the layer beneath it. `parseRaw` infers a value's type rather than taking a declared one: a hex string of exactly 20 bytes encodes as an `address` and one of exactly 32 bytes as a `bytes32`, while every other length encodes as dynamic `bytes`. That inference is what broke `readBytes` in v1.2.0, where it decoded `parseRaw` output as `bytes` instead of calling `vm.parseJsonBytes`. Reading through the dedicated cheatcode is correct today, so pin it: assert `readBytes` round-trips 19, 20, 21, 31, 32 and 33 byte values, bracketing both inference boundaries. Also document the inference on `parseRaw`, since reaching it through that function still reverts and nothing said so. Closes foundry-rs#592
developer3516
requested review from
0xrusowsky,
DaniPopes,
grandizzy,
mattsse and
onbjerg
as code owners
August 17, 2026 10:04
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.
Motivation
Closes #592.
The reported bug —
readBytesreturning wrong data or reverting on 20-bytevalues — does not reproduce on
master. It was real on forge-std v1.2.0, wherereadByteswasabi.decode(vm.parseJson(json, key), (bytes)). It now callsvm.parseJsonBytesdirectly, which sidesteps the cause.The cause itself is still there, one layer down.
parseRawinfers a value'stype instead of taking a declared one:
parseRawoutputbytesaddressbytes32So
abi.decode(json.parseRaw(key), (bytes))still reverts at exactly 20 and 32bytes.
readBytesavoids it, but nothing in the suite says so —readByteshasno test coverage at all — and nothing in the docs warns about it.
Solution
test_ReadBytesAtInferredTypeLengthsassertsreadBytesround-trips 19, 20,21, 31, 32 and 33 byte values, bracketing both inference boundaries. If
readBytesis ever routed back throughparseRaw, this fails.@devnote onparseRawdocuments the inference, so the next persondecoding its output as
bytesknows why it reverts.Testing
forge test: 207 passing, 0 failing.forge fmt --checkclean. Build matrix(stable,
--via-ir, solc 0.8.13 and 0.8.35, each with and without IR) all green.