Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/utils/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,11 @@ library LibBytes {
let s := calldataload(add(a.offset, offset)) // Relative offset of `result` from `a.offset`.
result.offset := add(a.offset, s)
result.length := sub(a.length, s)
if or(shr(64, or(s, or(l, a.offset))), gt(offset, l)) { revert(l, 0x00) }
// `gt(s, a.length)` is required: without it `sub(a.length, s)` underflows and
// `result.length` becomes ~2**256, yielding a slice that points past `a`.
// The sibling helpers already bound `s` -- `bytesInCalldata` via
// `gt(add(s, result.length), l)`, `staticStructInCalldata` via `gt(offset, l)`.
if or(shr(64, or(s, or(l, a.offset))), or(gt(offset, l), gt(s, a.length))) { revert(l, 0x00) }
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/LibBytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,25 @@ contract LibBytesTest is SoladyTest {
require(keccak256(expectedChildren[i]) == keccak256(children[i]));
}
}

function testDynamicStructInCalldataRejectsOutOfBoundsOffset() public {
// `s` (the relative offset read from calldata) greater than `a.length` used to make
// `sub(a.length, s)` underflow, returning a slice with a ~2**256 length pointing past `a`.
bytes memory encoded = abi.encodePacked(uint256(0x1000));
vm.expectRevert();
this.dynamicStructInCalldataAt(encoded, 0x00);
}

function dynamicStructInCalldataAt(bytes calldata a, uint256 offset)
public
pure
returns (uint256 o, uint256 l)
{
bytes calldata p = LibBytes.dynamicStructInCalldata(a, offset);
assembly {
o := p.offset
l := p.length
}
}

}
Loading