Skip to content

Commit

Permalink
mask values to be within 32 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
hensha256 committed Sep 4, 2024
1 parent e8cd67f commit 5eba3fb
Show file tree
Hide file tree
Showing 64 changed files with 74 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BaseActionsRouter_mock10commands.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62049
62100
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133506
133557
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135542
135593
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128016
128067
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128154
128205
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47468
47509
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 @@
47286
47327
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124115
124156
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123612
123653
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131193
131234
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130691
130732
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142671
142722
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151519
151570
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151519
151570
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withTakePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150879
150930
Original file line number Diff line number Diff line change
@@ -1 +1 @@
109612
109652
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117062
117113
Original file line number Diff line number Diff line change
@@ -1 +1 @@
116422
116473
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135220
135261
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127960
128000
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129778
129829
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 @@
117595
117646
Original file line number Diff line number Diff line change
@@ -1 +1 @@
155744
155795
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154746
154797
Original file line number Diff line number Diff line change
@@ -1 +1 @@
137544
137595
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133889
133940
Original file line number Diff line number Diff line change
@@ -1 +1 @@
174805
174856
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144761
144812
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
341266
341317
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349758
349809
Original file line number Diff line number Diff line change
@@ -1 +1 @@
349060
349111
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319248
319299
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319890
319941
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
245472
245523
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375290
375341
Original file line number Diff line number Diff line change
@@ -1 +1 @@
325266
325317
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
376566
376617
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
375706
375757
Original file line number Diff line number Diff line change
@@ -1 +1 @@
420993
421044
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8773
8798
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119801
119852
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118996
119047
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127868
127919
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134698
134749
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186239
186290
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
178174
178225
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
237761
237812
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229720
229771
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133506
133557
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118609
118660
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
117782
117833
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125530
125581
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119808
119859
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128680
128731
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133481
133532
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
185625
185676
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182475
182526
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
237765
237816
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
234639
234690
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
228917
228968
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132278
132329
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124327
124378
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
118618
118669
16 changes: 11 additions & 5 deletions src/libraries/CalldataDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ library CalldataDecoder {
/// @notice equivalent to SliceOutOfBounds.selector
bytes4 constant SLICE_ERROR_SELECTOR = 0x3b99b53d;

/// @notice mask used for offsets and lengths to ensure no overflow
/// @dev no sane abi encoding will pass in an offset or length greater than type(uint32).max
uint256 constant OFFSET_OR_LENGTH_MASK = 0xffffffff;

/// @dev equivalent to: abi.decode(params, (bytes, bytes[])) in calldata
function decodeActionsRouterParams(bytes calldata _bytes)
internal
Expand Down Expand Up @@ -246,19 +250,21 @@ library CalldataDecoder {
function toBytesArray(bytes calldata _bytes, uint256 _arg) internal pure returns (bytes[] calldata res) {
uint256 encodingLength;
assembly ("memory-safe") {
let bytesOffset := and(_bytes.offset, OFFSET_OR_LENGTH_MASK)
// The offset of the `_arg`-th element is `32 * arg`, which stores the offset of the length pointer.
// shl(5, x) is equivalent to mul(32, x)
let lengthPtr := add(_bytes.offset, calldataload(add(_bytes.offset, shl(5, _arg))))
let lengthPtr := and(add(bytesOffset, calldataload(add(bytesOffset, shl(5, _arg)))), OFFSET_OR_LENGTH_MASK)
// the number of byte strings in the byte array
let arrayLength := calldataload(lengthPtr)
let arrayLength := and(calldataload(lengthPtr), OFFSET_OR_LENGTH_MASK)
// pointer to encoding of the final bytes string in the array is `32 * arrayLength`
let finalElementLengthPtr := add(add(lengthPtr, 0x20), calldataload(add(lengthPtr, shl(5, arrayLength))))
let finalElementLengthPtr :=
and(add(add(lengthPtr, 0x20), calldataload(add(lengthPtr, shl(5, arrayLength)))), OFFSET_OR_LENGTH_MASK)
// the length of the final bytes string
let finalElementLength := calldataload(finalElementLengthPtr)
let finalElementLength := and(calldataload(finalElementLengthPtr), OFFSET_OR_LENGTH_MASK)
// the final bytes string's encoding is: 32 byte length, then the `length` bytes
let finalElementEnd := add(add(finalElementLengthPtr, finalElementLength), 0x20)
// total length needed in the calldata bytes array
encodingLength := sub(finalElementEnd, _bytes.offset)
encodingLength := sub(finalElementEnd, bytesOffset)

// the start of the bytes string encoding is the slot after the length
let arrayOffset := add(lengthPtr, 0x20)
Expand Down

0 comments on commit 5eba3fb

Please sign in to comment.