Skip to content

Commit

Permalink
Fix length check
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 6, 2024
1 parent e1501a8 commit c6397f0
Show file tree
Hide file tree
Showing 69 changed files with 84 additions and 77 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BaseActionsRouter_mock10commands.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
60529
60677
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129802
129854
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131842
131905
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124047
124110
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124189
124252
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50396
50440
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
50396
50440
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125497
125578
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124960
125025
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132348
132431
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131811
131878
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146180
146281
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154743
154847
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154743
154847
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withTakePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154084
154168
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111889
111970
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119624
119728
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118965
119049
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135151
135250
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128300
128397
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132311
132415
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_take_take.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120200
120304
Original file line number Diff line number Diff line change
@@ -1 +1 @@
159205
159032
Original file line number Diff line number Diff line change
@@ -1 +1 @@
158165
157972
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140884
140859
Original file line number Diff line number Diff line change
@@ -1 +1 @@
136268
136346
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177216
177339
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147898
148015
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
364754
364729
Original file line number Diff line number Diff line change
@@ -1 +1 @@
373256
373252
Original file line number Diff line number Diff line change
@@ -1 +1 @@
372500
372475
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
317750
317577
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
318420
318247
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
243989
243816
Original file line number Diff line number Diff line change
@@ -1 +1 @@
419141
419008
Original file line number Diff line number Diff line change
@@ -1 +1 @@
323781
323608
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
420303
420130
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
419381
419188
Original file line number Diff line number Diff line change
@@ -1 +1 @@
464471
464302
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2256
2259
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7994
8003
2 changes: 1 addition & 1 deletion .forge-snapshots/StateView_extsload_getPositionInfo.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2826
2829
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2543
2546
2 changes: 1 addition & 1 deletion .forge-snapshots/StateView_extsload_getTickInfo.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2758
2761
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7133
7148
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115522
115722
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115999
116043
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124814
124861
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130532
130584
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179540
179724
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170240
170577
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
228527
228843
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
219251
219720
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129802
129854
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
114792
114992
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115238
115282
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121750
121985
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117051
117107
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125866
125925
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129783
129870
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179611
179842
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
175495
175902
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229446
229821
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
225354
225905
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220655
221027
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129053
129140
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121020
121255
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116396
116452
25 changes: 16 additions & 9 deletions src/libraries/CalldataDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ library CalldataDecoder {
/// (note that this does deviate from standard solidity behavior and offsets/lengths will
/// be interpreted as mod type(uint32).max which will only impact malicious/buggy callers)
uint256 constant OFFSET_OR_LENGTH_MASK = 0xffffffff;
uint256 constant OFFSET_OR_LENGTH_MASK_WITH_PADDING = 0xffffffe0;
uint256 constant OFFSET_OR_LENGTH_MASK_AND_WORD_ALIGN = 0xffffffe0;

/// @notice equivalent to SliceOutOfBounds.selector, stored in least-significant bits
uint256 constant SLICE_ERROR_SELECTOR = 0x3b99b53d;
Expand All @@ -32,31 +32,38 @@ library CalldataDecoder {
// 0x00: 0x40 (offset to `actions.length`)
// 0x20: 0x60 + actions.length (offset to `params.length`)
// 0x40: `actions.length`
// 0x60: beginning of actions

// Verify actions offset matches strict encoding
let invalidData := xor(calldataload(_bytes.offset), 0x40)
actions.offset := add(_bytes.offset, 0x60)
actions.length := and(calldataload(add(_bytes.offset, 0x40)), OFFSET_OR_LENGTH_MASK)

let paramsLengthOffset := add(and(add(actions.length, 0x1f), OFFSET_OR_LENGTH_MASK_WITH_PADDING), 0x60)
// Verify actions offset matches strict encoding
// Round actions length up to be word-aligned, and add 0x60 (for the first 3 words of encoding)
let paramsLengthOffset := add(and(add(actions.length, 0x1f), OFFSET_OR_LENGTH_MASK_AND_WORD_ALIGN), 0x60)
// Verify params offset matches strict encoding
invalidData := or(invalidData, xor(calldataload(add(_bytes.offset, 0x20)), paramsLengthOffset))
let paramsLengthPointer := add(_bytes.offset, paramsLengthOffset)
params.length := and(calldataload(paramsLengthPointer), OFFSET_OR_LENGTH_MASK)
params.offset := add(paramsLengthPointer, 0x20)

// Expected head offset for `params[0]` is params.length * 32
// Expected offset for `params[0]` is params.length * 32
// As the first `params.length` slots are pointers to each of the array element lengths
let tailOffset := shl(5, params.length)
let expectedOffset := tailOffset

for { let offset := 0 } lt(offset, tailOffset) { offset := add(offset, 32) } {
let cdOffsetItemLength := calldataload(add(params.offset, offset))
invalidData := or(invalidData, xor(cdOffsetItemLength, expectedOffset))
let cdPtrItemLength := add(params.offset, cdOffsetItemLength)
let itemLengthOffset := calldataload(add(params.offset, offset))
// Verify that the offset matches the expected offset from strict encoding
invalidData := or(invalidData, xor(itemLengthOffset, expectedOffset))
let itemLengthPointer := add(params.offset, itemLengthOffset)
let length :=
add(and(add(calldataload(cdPtrItemLength), 0x1f), OFFSET_OR_LENGTH_MASK_WITH_PADDING), 0x20)
add(and(add(calldataload(itemLengthPointer), 0x1f), OFFSET_OR_LENGTH_MASK_AND_WORD_ALIGN), 0x20)
expectedOffset := add(expectedOffset, length)
}

if invalidData {
// if the data encoding was invalid, or the provided bytes string isnt as long as the encoding says, revert
if or(invalidData, lt(add(_bytes.length, _bytes.offset), add(params.offset, expectedOffset))) {
mstore(0, SLICE_ERROR_SELECTOR)
revert(0x1c, 4)
}
Expand Down

0 comments on commit c6397f0

Please sign in to comment.